1. 空構造器實例化:
<bean id="helloServiceNoWithArgs" class="cn.lovepi.***.HelloWorldImpl" />
2. 有參數構造器實例化:
<bean id="helloServiceWithArgs" class=" cn.lovepi.***.HelloWorldmpl">
<!-- 指定構造器參數 -->
<constructor-arg index="0" value="Hello Spring!"/>
</bean>
public interface HelloWorld {
public void sayHello();
}
接下來爲這個接口創建一個實現類HelloWorldImpl:
public class HelloWorldImpl implements HelloWorld{
private String message;
/**
* 空構造器
*/
HelloWorldImpl(){
message="Hello World";
}
/**
* 帶參數的構造器
* @param message
*/
HelloWorldImpl(String message){
this.message=message;
}
public void sayHello() {
System.out.println(message);
}
}
編寫配置文件conf-instance.xml:<!--使用默認構造參數-->
<bean id="helloWorldWithNoArgs"
class="cn.lovepi.chapter03.instance.HelloWorldImpl" />
<!--使用有參數構造參數-->
<bean id="helloWorldWithArgs"
class="cn.lovepi.chapter03.instance.HelloWorldImpl" >
<!-- 指定構造器參數 -->
<constructor-arg index="0" value="Hello Args!"/>
</bean>
/**
* 使用無參數的構造函數來實例化bean
*/
public static void sayHelloWordWithNoArgs(){
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-instance.xml");
HelloWorld helloWorld=factory.getBean("helloWorldWithNoArgs",HelloWorldImpl.class);
helloWorld.sayHello();
}
/**
* 使用有參數的構造函數來實例化bean
*/
public static void sayHelloWorldWithArgs(){
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-instance.xml");
HelloWorld helloWorld=factory.getBean("helloWorldWithArgs",HelloWorldImpl.class);
helloWorld.sayHello();
}
<!--使用有參數構造參數-->
<bean id="helloServiceStaticFactory" class="cn.lovepi.***.HelloWorldStaticFactory"
factory-method="newInstance">
<!-- 指定構造器參數 -->
<constructor-arg index="0" value="Hello Static Factory!"/>
</bean>
public class HelloWorldStaticFactory {
/**
* 工廠方法
* @param message
* @return
*/
public static HelloWorld newInstance(String message){
//返回帶參數的HelloWorldImpl構造的helloWorld實例
return new HelloWorldImpl(message);
}
}
接下來在配置文件中來配置當前bean
<!--靜態工廠方式-->
<bean id="helloWorldStaticFactory"
class="cn.lovepi.chapter03.instance.HelloWorldStaticFactory"
factory-method="newInstance" >
<!-- 指定構造器參數 -->
<constructor-arg index="0" value="Hello Static Factory!"/>
</bean>
在Main中實例化Beanpublic static void sayHelloWordStaticFactory(){
//讀取配置文件,實例化一個IoC容器
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-instance.xml");
//從容器中獲取bean,注意此處完全“面向接口編程而不是面向實現”
HelloWorld helloWorld=factory.getBean("helloWorldStaticFactory",HelloWorld.class);
//執行業務邏輯
helloWorld.sayHello();
}
<!-- 1、定義實例工廠Bean -->
<bean id="beanInstanceFactory" class="cn.lovepi.***.HelloWorldInstanceFactory" />
<!-- 2、使用實例工廠Bean創建Bean -->
<bean id=“helloWorldInstance" factory-bean="beanInstanceFactory"
factory-method="newInstance">
<constructor-arg index="0" value="Hello Instance Factory!"></constructor-arg>
</bean>
public class HelloWorldInstanceFactory {
/**
* 工廠方法
* @param message
* @return
*/
public HelloWorld newInstance(String message){
//需要返回的bean實例
return new HelloWorldImpl(message);
}
}
在配置文件中配置相關屬性:<!-- 1、定義實例工廠Bean -->
<bean id="helloWorldInstanceFactory"
class="cn.lovepi.chapter03.instance.HelloWorldInstanceFactory" />
<!-- 2、使用實例工廠Bean創建Bean -->
<bean id="helloWorldInstance"
factory-bean="helloWorldInstanceFactory"
factory-method="newInstance">
<constructor-arg index="0"
value="Hello Instance Factory!">
</constructor-arg>
</bean>
public static void sayHelloWorldInstanceFactory(){
//讀取配置文件,實例化一個IoC容器
BeanFactory factory=
new FileSystemXmlApplicationContext("src/conf/conf-instance.xml");
//從容器中獲取bean,注意此處完全“面向接口編程而不是面向實現”
HelloWorld helloWorld=factory.getBean("helloWorldInstance",HelloWorld.class);
//執行業務邏輯
helloWorld.sayHello();
}