Spring-IoC的基本使用

Spring

Spring简介

Spring介绍

Spring是一个非常活跃的开源框架,它基于IOC和AOP来架构多层JavaEE系统,以帮助分离项目间组件的相互依赖关系。它的主要目的是简化企业开发。

Spring解决的问题

方便解耦,简化开发:可以把所有对象的创建和依赖关系维护交给Spring来管理。

AOP编程:Spring是面向切面编程的,可以方便的对应用程序进行权限拦截、运行监控。

声明式事务的支持:只需要配置好相关的配置文件就可以实现对事务的管理,不需要手动编程。

方便的程序测试:Spring对Junit4的支持,通过注解的方式方便的对Spring程序进行测试。

方便集成各种优秀的框架:Spring不排斥其他优秀的开源框架,期内部提供了对许多优秀的框架的直接支持。

降低了JavaEE API的使用难度:对JavaEE中一些难使用的API都进行了封装,降低了这些API的使用难度。

Spring的组成

Spring的功能大约由20个模块组成,这些模块按组可分为核心容器、数据访问/集成、Web、AOP、设备、消息、测试。

Spring的组成图


入门程序和IOC简介

依赖注入和反转控制的定义中,调用者不负责被调用者的实例创建的工作,该工作由Spring框架中的容器完成,它通过开发者的配置来确定实例类型,实例化对象后再注入调用者。

IOC控制反转

降低对象间的耦合关系的设计思想,开发者不再关注对象的创建,交给Spring来完成。

缺点:因为对象是通过反射的机制来完成创建的,对性能有一定的影响。

DI依赖注入

Denpendency Injection 说的是创建实例化对象时,同时为对象注入其依赖的属性。将bean与bean的关系交给容器管理,这个容器就是Spring。

IOC和DI

所谓依赖注入,说的是IOC容器在运行的过程中,动态的将某种依赖关系注入到对象中。

所以依赖注入(DI)和控制反转(IOC)是从两个不同的方向描述的同一件事,就是通过引入IOC,利用依赖注入的方式,实现对象间的解耦。

练习

导入jar包

通过配置pom.xml实现jar包的导入

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.8.Release</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>
​
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>
​
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>
​
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.2</version>
    </dependency>
​
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
 </dependency>
创建操作类
package com.xiaoyao.spring.bean;
​
​
public class Person {
​
    private String name;
    private Integer age;
​
    public Person() {
        System.out.println("------------> Person.Person");
    }
}
创建配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
​
        <!-- 
             id 和 name 可以同时存在,作为bean的标识
             class添加的应该是class的全路径
         -->
       <bean
           id="personId" name="personName" class="com.itqf.spring.bean.Person"
        />
​
</beans>
测试用例
@Test
 public void test1(){
        //TODO 测试IOC
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //getBean 可以使用配置文件中的id值,也可以使用配置文件的name值.
        Person person = (Person) applicationContext.getBean("personId");
        System.out.println("person = " + person);
  }

对象创建的细节

bean标签属性
Property 属性解释
class 指定bean对应类的全路径
name name是bean对应对象的一个标识
scope 执行bean对象创建模式和生命周期
id id是bean对象的唯一标识,不能添加特别字符
lazy-init 是否延时加载 默认值:false
init-method 对象初始化方法
destory 对象销毁方法
创建对象工厂
public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
bean标签

id和name的作用几乎相同,但是还是有细微的差别,id不允许重复,不允许使用特殊的字符。

<!--同时添加name和id -->
<bean name="testBeanName"  id="testBeanId"  class="com.xiaoyao.spring.bean.TestBean" />

获取bean

ApplicationContext applicationContext = new  ClassPathXmlApplicationContext("applicationContext-bean.xml");
        /**
         * 参数1: name/id
         * 参数2(可选): 可以指定生成对象类型,如果不填此参数,需进行强转
         * 两种方式都可以获取!
         */
        TestBean testBeanName = applicationContext.getBean("testBeanName",  TestBean.class);
        TestBean testBeanId = applicationContext.getBean("testBeanId",  TestBean.class);

Scope属性

用于设置bean对应对象的实例化规则,可以设置为:singleton和prototype

singleton为默认值,默认在Spring容器启动时,实例化bean对应的对象。只会实例化一次,后面使用的对象为同一个对象。

<bean name="testBeanName" scope="singleton" id="testBeanId"  class="com.itqf.spring.bean.TestBean" />

prototype在调用getBean时实例化bean对应的对象。每次getBean时都会实例化一个对象,每个对象不相同。

<bean name="testBeanName" scope="prototype" id="testBeanId"  class="com.itqf.spring.bean.TestBean" />
lazy-init属性(是否延时初始化)

只对Scope设置为Singleton时有效。

lazy-init设置为true时,scope=singleton,延时初始化bean对应的对象。

<bean name="testBeanName" id="testBeanId"  scope="singleton" lazy-init="true" class="com.itqf.spring.bean.TestBean" />

lazy-init设置为false时,scope=singleton,不延时初始化bean对应的对象,在Spring容器启动时初始化。

<bean name="testBeanName" id="testBeanId"  scope="singleton" lazy-init="false" class="com.itqf.spring.bean.TestBean" />

对象创建的几种方式

静态工厂

<bean name="personFactory" class="com.itqf.spring.utils.PersonFactory" factory-method="createPerson" />

非静态工厂

<bean name="personFactory1" class="com.itqf.spring.utils.PersonFactory" />
<bean name="personFactory2"  factory-bean="personFactory1" factory-method="createPerson1" />



猜你喜欢

转载自blog.csdn.net/Zyxiaoyao/article/details/80627188