Spring IOC 容器 Bean 对象实例化

Spring IOC 容器 Bean 对象实例化

构造器实例化

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

1.设置配置文件

<?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"/>

</beans>

2.编写Bean对象

UserService.java

package com.xxxx.service;

public class UserService {
    
    

    //带参构造
    public UserService(String name){
    
    }


    public void test(){
    
    
        System.out.println("UserService...");
    }
}

3.加载配置文件,获取实例化对象

package com.xxxx;

import com.xxxx.service.UserService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring IOC Bean对象实例化
 *      1.构造器实例化
 *          默认使用构造器实例化,需要Bean对象提供空构造,如果没有空构造,则创建失败
 */

public class Starter {
    
    
    public static void main(String[] args) {
    
    

        //加载spring上下文环境
        BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
        //得到bean对象
        UserService userService = (UserService) factory.getBean("userService");
        userService.test();
    }
}

运行结果:

Failed to instantiate [com.xxxx.service.UserService]: No default constructor found;

去掉带参构造,再次运行:

UserService...

静态工厂实例化

要有该工厂类及工厂方法
工厂方法为静态的

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

1.定义静态工厂类

package com.xxxx.factory;


import com.xxxx.service.UserService;

/**
 * 静态工厂类
 *      1.定义静态工厂类
 *      2.定义静态方法,并返回需要被实例化的Bean对象
 */
public class StaticFactory {
    
    
    /**
     * 静态方法
     */
    public static UserService createService(){
    
    
        System.out.println("静态工厂实例化...");
        return new UserService();
    }

}

2.设置配置文件

<?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对象)-->
<!--设置工厂调用的方法-->
<bean id="userService" class="com.xxxx.factory.StaticFactory" factory-method="createService"/>

</beans>

3.加载配置文件,获取实例化对象

package com.xxxx;

        import com.xxxx.service.UserService;
        import org.springframework.beans.factory.BeanFactory;
        import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Spring IOC Bean对象实例化
 *      1.构造器实例化
 *          默认使用构造器实例化,需要Bean对象提供空构造,如果没有空构造,则创建失败
 * 		2.静态工厂实例化
 * 			
 */

public class Starter {
    
    
    public static void main(String[] args) {
    
    

        //加载spring上下文环境
        BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
        //得到bean对象
        UserService userService = (UserService) factory.getBean("userService");
        userService.test();
    }
}

运行结果:

静态工厂实例化...
UserService...

实例化工厂实例化

⼯⼚⽅法为⾮静态⽅法
需要配置⼯⼚bean,并在业务bean中配置factory-bean,factory-method属性

1.定义工厂类

package com.xxxx.factory;

import com.xxxx.service.UserService;

public class InstanceFactory {
    
    
    /**
     * 定义方法,返回实例化对象
     */
    public UserService createUserService(){
    
    
        System.out.println("实例化工厂实例化...");
        return new UserService();
    }
}

2.设置配置文件

<?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"/>
<!--factory-bean:对应的工厂类的id属性值-->
<!--factory-method:工厂类中方法名-->
    <bean id="userService" factory-bean="instanceFactory" factory-method="createUserService"/>

</beans>

3.加载配置文件,获取实例化对象

package com.xxxx;

        import com.xxxx.service.UserService;
        import org.springframework.beans.factory.BeanFactory;
        import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Starter {
    
    
    public static void main(String[] args) {
    
    

        //加载spring上下文环境
        BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
        //得到bean对象
        UserService userService = (UserService) factory.getBean("userService");
        userService.test();
    }
}

运行结果:

实例化工厂实例化...
UserService...

三种实例化Bean的方式比较

  • 方式⼀:通过bean的缺省构造函数创建,当各个bean的业务逻辑相互比较独立的时候或者和外界关联较少的时候可以使用。
  • 方式⼆:利用静态factory方法创建,可以统⼀管理各个bean的创建,如各个bean在创建之前需要相同的初始化处理,则可⽤这个factory方法险进行统⼀的处理等等。
  • 方式三:利用实例化factory方法创建,即将factory方法也作为了业务bean来控制,1可用于集成其他框架的bean创建管理方法,2能够使bean和factory的角色互换。

猜你喜欢

转载自blog.csdn.net/lln1540295459/article/details/121098130