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都是不一樣的