Spring三种实例化Bean的方式比较

51 min read

构造器实例化

注:通过默认构造器创建 空构造方法必须存在 否则创建失败

  1. 设置配置文件 spring.xml

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="userService" class="com.xxxx.service.UserService"></bean>
    
    </beans>
    
  2. 获取实例化对象

    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    UserService userService = (UserService) ac.getBean("userService");  
    userService.test();
    

静态工厂实例化

注:

  • 要有该工厂类及工厂方法
  • 工厂方法为静态的
  1. 定义静态工厂类

    package com.xxxx.factory;
    
    import com.xxxx.service.UserService;
    
    /**
     * 定义静态工厂类
     */
    public class StaticFactory {
        /**
         * 定义对应的静态方法,返回实例化对象
         * @return
         */
        public static UserService createUserService() {
            return new UserService();
        }
    }
    
    
  2. 设置配置文件 spring.xml

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--静态工厂-->
        <bean id="userService" class="com.xxxx.factory.StaticFactory" factory-method="createUserService"></bean>
    
    </beans>
    
  3. 获取实例化对象

    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    UserService userService = (UserService) ac.getBean("userService");  
    userService.test();
    

    ​ 当我们指定Spring使用静态工厂方法来创建Bean实例时,Spring将先解析配置文件,并根据配置文件指定的信息,通过反射调用静态工厂类的静态工厂方法,并将该静态工厂方法的返回值作为Bean实例,在这个过程中,Spring不再负责创建Bean实例,Bean实例是由用户提供的静态工厂方法提供的。

实例化工厂实例化

注:

  • 工厂方法为非静态方法
  • 需要配置工厂bean,并在业务bean中配置factory-bean,factory-method属性
  1. 定义工厂类

    package com.xxxx.factory;
    
    import com.xxxx.service.UserService;
    
    /**
     * 定义工厂类
     */
    public class InstanceFactory {
        /**
         * 定义方法,返回实例化对象
         * @return
         */
        public UserService createUserService() {
            return new UserService();
        }
    }
    
  2. 设置配置文件 spring.xml

    <?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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--
    		实例化工厂
    			1.定义实例化工厂bean
        		2.引用工厂bean 指定工厂创建方法(方法为非静态)
    	-->
        <bean id="instanceFactory" class="com.xxxx.factory.InstanceFactory"></bean>
        <bean id="userService" factory-bean="instanceFactory" factory-method="createUserService"></bean>
    
    </beans>
    
  3. 获取实例化对象

    ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
    UserService userService = (UserService) ac.getBean("userService");  
    userService.test();
    

Spring三种实例化Bean的方式比较

  • 方式一:通过bean的缺省构造函数创建,当各个bean的业务逻辑相互比较独立的时候或者和外界关联较少的时候可以使用。

  • 方式二:利用静态factory方法创建,可以统一管理各个bean的创建,如各个bean在创建之前需要相同的初始化处理,则可用这个factory方法险进行统一的处理等等。

  • 方式三:利用实例化factory方法创建,即将factory方法也作为了业务bean来控制,1可用于集成其他框架的bean创建管理方法,2能够使bean和factory的角色互换。

    开发中项目一般使用一种方式实例化bean,项目开发基本采用第一种方式,交给Spring托管,使用时直接拿来使用即可。另外两种了解