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中实例化Bean
public 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(); }