Spring核心功能 - IOC

目录说明

1 What is the IOC ?


IOC(Inversion Of Control):即控制反转,也被称为依赖注入。控制反转就是应用本身不负责依赖对象的创建和维护,将对象的创建和维护交给外部容器处理。这样应用的控制权就交给了外部容器,控制权的转移就是控制反转,目的是为了获得更好的维护性以及降低耦合度。

2 Why use IOC ?


IOC 容器可以用来管理应用对象的配置和生命周期,配置每个对象的创建和销毁,也可以配置每个 bean 是只有一个实例,还是每次需要时都生成一个新的实例(即singleton or prototype),以及它们是如何相互关联的。

3 How to use IOC


3.1 环境搭建

【1】 导入 Spring 核心 jar 文件(最好使用版本 3.0+)

commons-logging-1.1.3.jar           日志
spring-beans-3.2.5.RELEASE.jar        bean节点
spring-context-3.2.5.RELEASE.jar       spring上下文节点
spring-core-3.2.5.RELEASE.jar         spring核心功能
spring-expression-3.2.5.RELEASE.jar    spring表达式相关表

【2】核心配置文件:applicationContext.xml / bean.xml

bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
</beans>   

【3】一个简单的 IOC 容器的获取

1 创建 User.class

class User{
  private Integer id;
  private String name;

setter .... getter
}

2 bean.xml 配置

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
      <bean id="user" class="cn.acey1.User"></bean>
</beans>   

3 获取 IOC 容器中的 User 对象

public class test{
    @Test
    public void testAc() throws Exception {
        // 得到IOC容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("cn/acey1/bean.xml");
        // 从容器中获取bean
        User user = (User) ac.getBean("user");
        
        System.out.println(user);
    }
}
3.2 bean 节点的说明

在 IOC 容器中存在多个 bean ,每个 bean 都对应着一个对象,当需要获取该对象时,直接从 IOC 容器中取。bean 节点中的几个属性说明

<bean id=" " 
      class=" " 
      scope=“ ” 
      lazy-init=“ ”
      init-method=“ ”
      destroy-method=“ ” 
/>
  • id : 在 IOC 容器中每个 bean 都对应着一个标示即id
  • class:id 对应的类
  • scope:创建方式,默认是 singleton
  • singleton:单例,在容器启动之前就创建 bean,在整个应用只有一个
  • prototype:多例,在用到对象的时候就创建,整个应用中存在多个
  • lazy-init:是否延迟创建(只对 singleton 有效),默认是 false
  • false:不延迟创建,在启动的时候就创建
  • true:延迟创建,在使用到的时候在创建
  • init-method:在对象创建时执行该方法,该方法必须在该类中存在
  • destory-method :当容器销毁时执行的方法,该方法必须在该类中存在
3.3 对象创建的几种方式

【1】使用无参构造器

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
      <bean id="user" class="cn.acey1.User"></bean>
</beans>   

【2】使用有参构造器

User.class

public class User{
 public User(int id , String name){
   ...
}
}

bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
      <bean id="user" class="cn.acey1.User">
         <constructor-arg index="0" type="int" value="100"></constructor-arg>
        <constructor-arg index="1" type="java.lang.String" value="Acey">
        </constructor-arg>
     </bean>
</beans>   

【3】工厂类创建:静态和非静态

UserFactory.class

public class UserFactory{

      //实例方法创建 User
       public User getUser(){
          return new User();
       }


      //静态方法创建 User
       public static User getStaticUser(){
          return new User();
       }
}

bean.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

      <!-- #实例创建 -->
      <!-- 先创建工厂 -->
     <bean id="factory" class="cn.acey1.UserFactory"></bean>
    <!-- 在创建user对象,用factory的实例方法 -->
    <bean id="user" factory-bean="factory" factory-method="getUser"></bean>
    
    
    <!--  #静态方法创建 -->
    <!-- 
        class 指定的就是工厂类型
        factory-method  一定是工厂里面的“静态方法”
     -->
    <bean id="user" class="cn.acey1.UserFactory" factory-method="getStaticUser"></bean>
</beans>   

3.4 对象关系依赖(依赖注入)

【1】通过构造函数注入

【2】通过 set 方法注入

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

   
      <!-- 基本类型注入 -->
     <bean id="user" class="cn.acey1.User">
        <property name="id" value="1">
        <property name="name" value="Acey">
     </bean>

      <!-- 引用类型注入 -->
    <bean id="str" class="java.lang.String">
        <constructor-arg value="Acey"></constructor-arg>
    </bean>
     <bean id="user" class="cn.acey1.User">
        <property name="name" ref="str">
     </bean>    
</beans>   

【3】p 名称空间注入(优化) Spring3.0+支持

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="str" class="java.lang.String">
        <constructor-arg value="Acey"></constructor-arg>
    </bean>
     <bean id="user" class="cn.acey1.User"  p:ref-name="str"></bean>    
</beans>   

【4】注解

使用步骤:

  1. 引入 context 名称空间
    xmlns:context="http://www.springframework.org/schema/context"
  2. 开启注解扫描
    <context:component-scan base-package="cn.Acey"></context:component-scan>
  3. 使用注解
  • @Component 把指定的对象加入 IOC 容器
  • @scope("[ prototype / singleton]") 创建方式(多例/单例(default))
  • @Repository 作用同 @Component,一般在持久层使用
  • @Service 作用同 @Component,一般在业务层使用
  • @Controller 作用同 @Component,一般在控制层使用
  • @Resource 属性注入
<!--持久层--!>
@Repository(value="userDao")
//等价于<bean id="userDao" class="cn.acey1.UserDao"></bean>
或者
@Repository   //如果不指定 value 值,那么默认的 bean 名字为该类名,其中类名的首字母小写
public class UserDao{
...
}

<!--业务层--!>
@Service
public class UserService{

    @Resource(name="userDao") //如果不指定 name 值,那么默认值就为属性名
     private UserDao userDao;
}

<!--控制  层--!>
@Controller
@scope("prototype")//多例模式
public class UserAction{

}

所有实例代码地址 https://github.com/Aceysx/SpringDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容