spring提供了三种bean实例的定义方式:
- 构造器实例化bean
- 静态工厂方法实例化bean:本质是直接调用: 类.静态方法
- 实例工厂方法实例化bean:本质:先生成工厂bean,然后用工厂bean创建实例
示例方法
- 参照上上篇文章描述,创建maven工程,引入spring的maven依赖包
- 创建测试类:
package ioc2;
public interface HelloWorld {
void sayHelloWorld();
}
class HelloWorldImpl implements HelloWorld {
String message;
public HelloWorldImpl() {
this.message = "Hello World";
}
public HelloWorldImpl(String msg) {
this.message = msg;
}
@Override
public void sayHelloWorld() {
System.out.println(message);
}
}
class HelloWorldFactory {
public static HelloWorld newInstance() {
return new HelloWorldImpl();
}
public static HelloWorld newInstance(String message) {
return new HelloWorldImpl(message);
}
public HelloWorld newInstance2(String message) {
return new HelloWorldImpl(message);
}
}
- 创建spring的xml配置文件:ioc2-construct.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<bean id="helloWorldDefault" class="ioc2.HelloWorldImpl"/>
<bean id="helloWorldWithArgs" class="ioc2.HelloWorldImpl">
<constructor-arg index="0" value="Hello with args"/>
</bean>
<bean id="HelloWorldStatic1" class="ioc2.HelloWorldFactory" factory-method="newInstance"/>
<bean id="HelloWorldStatic2" class="ioc2.HelloWorldFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello From Static"/>
</bean>
<bean id="HelloWorldFactory" class="ioc2.HelloWorldFactory"/>
<bean id="HelloWorldByFactoryInstance" factory-bean="HelloWorldFactory" factory-method="newInstance2">
<constructor-arg index="0" value="Hello From BeanFactoryInstance"/>
</bean>
</beans>
class Main {
public static void main(String[] args) {
BeanFactory beanFactory = new ClassPathXmlApplicationContext("ioc2-construct.xml");
System.out.println("========== 1.1 声明一个bean, 使用默认构造器 ==========");
HelloWorld helloWorld11 = beanFactory.getBean("helloWorldDefault", HelloWorld.class);
helloWorld11.sayHelloWorld();
System.out.println("========== 1.2 声明一个bean, 指定构造器 ==========");
HelloWorld helloWorld12 = beanFactory.getBean("helloWorldWithArgs", HelloWorld.class);
helloWorld12.sayHelloWorld();
System.out.println("========== 2.1 声明一个bean, 使用静态工厂:无参 ==========");
HelloWorld helloWorld21 = beanFactory.getBean("HelloWorldStatic1", HelloWorld.class);
helloWorld21.sayHelloWorld();
System.out.println("========== 2.2 声明一个bean, 使用静态工厂:有参 ==========");
HelloWorld helloWorld22 = beanFactory.getBean("HelloWorldStatic2", HelloWorld.class);
helloWorld22.sayHelloWorld();
System.out.println("========== 3. 通过实例工厂处理 ==========");
HelloWorld helloWorld3 = beanFactory.getBean("HelloWorldByFactoryInstance", HelloWorld.class);
helloWorld3.sayHelloWorld();
System.out.println("========== 可以看到:三种方式都可以生成HelloWorld的bean,并且地址都不一样 ==========");
System.out.println(helloWorld11);
System.out.println(helloWorld12);
System.out.println(helloWorld21);
System.out.println(helloWorld22);
System.out.println(helloWorld3);
}
}
========== 1.1 声明一个bean, 使用默认构造器 ==========
Hello World
========== 1.2 声明一个bean, 指定构造器 ==========
Hello with args
========== 2.1 声明一个bean, 使用静态工厂:无参 ==========
Hello World
========== 2.2 声明一个bean, 使用静态工厂:有参 ==========
Hello From Static
========== 3. 通过实例工厂处理 ==========
Hello From BeanFactoryInstance
========== 可以看到:三种方式都可以生成HelloWorld的bean,并且地址都不一样 ==========
ioc2.HelloWorldImpl@564718df
ioc2.HelloWorldImpl@51b7e5df
ioc2.HelloWorldImpl@18a70f16
ioc2.HelloWorldImpl@62e136d3
ioc2.HelloWorldImpl@c8e4bb0
- 结果说明:
- 三种方式都可以实例化HelloWorldImpl实例bean,并且都可以调用
- 三种方式生成的bean都是不一样的