Spring IoC容器如何实例化Bean呢?传统应用程序可以通过new和反射机制进行实例化Bean。而Spring Ioc容器则需要根据Bean定义里的配置元数据使用反射机制来创建Bean。在Spring IoC容器中主要有以下几种创建Bean实例的方式:
使用构造器实例化Bean
使用静态工厂方式实例化Bean
使用实例工厂方式实例化Bean
构造器实例化
构造器实例化Bean是最简单的方式,SpringIoC容器既能使用默认空构造器也能使用有参数构造器两种方式构造Bean,如下方式指定要创建的Bean类型:
1.空构造器实例化:
<bean id="helloServiceNoWithArgs"class="com.bestone.***.HelloWorldImpl"/>
2.有参数构造器实例化:
<beanid="helloServiceWithArgs"class="com.bestone.***.HelloWorldImpl">
<!--比无参多了指定构造器参数-->
<constructor-arg index="0" value="HelloSpring!"/>
</bean>
Conf-instance.xml配置文件
<!--使用默认构造参数-->
<bean id="helloWorldWithNoArgs" class="com.bestone.spring.chapter04.instance.HelloWorldImpl"/>
<!--使用有参数构造参数-->
<bean id="helloWorldWithArgs" class="com.bestone.spring.chapter04.instance.HelloWorldImpl">
<!-- 指定构造器参数 -->
<constructor-arg index="0" value="Hello Args!"/>
</bean>
Main.java文件
public static void main(String[] args) {
sayHelloWithArgs();
sayHelloWithNoArgs();
}
//使用无参数构造器来实例化Bean
public static void sayHelloWithNoArgs() {
BeanFactory beanFactory =
newClassPathXmlApplicationContext("conf/conf-instance.xml"); //先加载配置文件,并且启动IoC容器
HelloWorld helloWorld = beanFactory.getBean("helloWorldWithNoArgs",HelloWorld.class);
//通过beanFactory获取不带构造参数的Bean的实例
helloWorld.sayHello(); //利用这个实例输出信息 HelloWorld!
}
//使用有参数构造器来实例化Bean
public static void sayHelloWithArgs() {
BeanFactory beanFactory =
newClassPathXmlApplicationContext("conf/conf-instance.xml");
HelloWorld helloWorld = beanFactory.getBean("helloWorldWithArgs",HelloWorld.class);
//通过beanFactory获取带构造参数的Bean的实例
helloWorld.sayHello(); //利用这个实例输出信息 Hello Args!
}
静态工厂方式实例化
使用静态工厂的方式除了指定必须的class属性,还要指定facotry-method属性来指定实例化Bean的方式,而且使用静态工厂方法也允许指定方法参数(构造注入),SpringIoC容器将调用此属性指定的方法来获取Bean,配置如下:
<!--使用有参数构造参数 -->
<bean id="helloServiceStaticFactory" class="com.bestone.***.HelloWorldStaticFacory" factory-method="newInstance">
<!--指定构造器参数-->
<constructor-arg index="0" value="Hello StaticFactory!"/>
</bean>
Conf-instance.xml配置文件
<bean id="helloWorldStaticFactory"
class="com.bestone.spring.chapter04.instance.HelloWorldStaticFactory"
factory-method="newInstance">
<!-- 指定构造器参数 -->
<constructor-arg index="0"value="Hello Static Factory!"/>
</bean>
Main.java
//使用静态工厂方法来实例化Bean
public static voidhelloWorldStaticFactory() {
// 1、读取配置文件实例化一个IOC容器
BeanFactory beanFactory=
newClassPathXmlApplicationContext("conf/conf-instance.xml");
// 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现”
HelloWorld helloWorld
=beanFactory.getBean("helloWorldStaticFactory", HelloWorld.class);
// 3、执行业务逻辑
helloWorld.sayHello();
}
HelloWorldStaticFacotry.java
public static HelloWorld newInstance(Stringmessage) {
// 返回需要的Bean实例
return new HelloWorldImpl(message);
}
实例工厂方式实例化
使用实例工厂方式不能指定class属性,此时必须使用 factory-bean属性来指定工厂Bean,factory-method属性指定实例化Bean的方法,而且使用实例工厂方法允许指定方法参数,方式和使用构造器方式一样,配置如下:
<!-- 1.定义实例工厂Bean-->
<bean id="beanInstanceFactory" class="com.bestone.***.helloWorldInstanceFactory"/>
<!--2.使用实例工厂Bean创建Bean-->
<bean id="helloWorldInstance" factory-bean="beanInstanceFactory" Factory-method="newInstance">
<constuctor-arg index="0" value="Hello InstanceFactory!></constructor-arg>
</bean>
Conf-instance.xml配置文件
<!-- 1、定义实例工厂Bean -->
<bean id="helloWorldInstanceFactory"
class="com.bestone.spring.chapter04.instance.HelloWorldInstanceFactory"/>
<!-- 2、使用实例工厂Bean创建Bean -->
<bean id="helloWorldInstance"factory-bean="helloWorldInstanceFactory"factory-method="newInstance">
<constructor-argindex="0" value="Hello InstanceFactory!"></constructor-arg>
</bean>
HelloWorldSInstanceFacotry.java
public HelloWorldnewInstance(String message) {
// 返回需要的Bean实例
return new HelloWorldImpl(message);
}
Main.java
//使用实例化工厂方法来实例化Bean
public static voidhelloWorldInstanceFactory() {
// 1、读取配置文件实例化一个IOC容器
BeanFactory beanFactory=
newClassPathXmlApplicationContext("conf/conf-instance.xml");
// 2、从容器中获取Bean,注意此处完全“面向接口编程,而不是面向实现编程”
HelloWorld helloWorld
=beanFactory.getBean("helloWorldInstance", HelloWorld.class);
// 3、执行业务逻辑
helloWorld.sayHello();
}
}