实例化bean的三种方式
构造器实例化bean
Person.java
1 public class Person { 2 3 private String name; 4 private Integer age; 5 6 public Person() { 7 System.out.println("这是一个无参构造函数"); 8 } 9 10 public Person(String name) { 11 this.name = name; 12 System.out.println("带有name="+name+"参数的构造函数"); 13 } 14 15 public Person(String name, Integer age) { 16 this.name = name; 17 this.age = age; 18 System.out.println("带有名字="+name+"和年龄="+age+"参数的构造函数"); 19 } 20 21 }
applicationContext.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <bean id="person" class="com.dfdc.spring.demo.Person"></bean> 7 <bean id="personWithParam" class="com.dfdc.spring.demo.Person"> 8 <constructor-arg name="name" value="等风的草"/> 9 </bean> 10 <bean id="personWirhParams" class="com.dfdc.spring.demo.Person"> 11 <constructor-arg name="name" value="等风的草"/> 12 <constructor-arg name="age" value="21"/> 13 </bean> 14 </beans>
以下将省略DTD部分。
测试方法:
1 //以下将省略这行 2 private static final String configLocation = "applicationContext.xml"; 3 4 /* 5 *构造函数实例化bean 6 */ 7 @Test 8 public void constructTest() { 9 ApplicationContext context = new ClassPathXmlApplicationContext(configLocation); 10 //Person p = (Person) context.getBean("person");//不带参数 11 12 //Person p = (Person) context.getBean("personWirhParam");//带一个参数 13 14 Person p = (Person) context.getBean("personWirhParams");//带多个参数 15 }
结果:
静态工厂实例化bean
当采用静态工厂方法创建bean时,除了需要指定class属性外,还需要通过factory-method属性来指定创建bean实例的工厂方法。
Spring将调用此方法返回实例对象,就此而言,跟通过普通构造器创建类实例没什么两样。
下面的bean定义展示了如何通过工厂方法来创建bean实例。
注意,此定义并未指定返回对象的类型,仅指定该类包含的工厂方法。在此例中,createInstance()必须是一个static方法。
1 <bean id="personStaticFactory" class="com.dfdc.spring.demo.PersonStaticFactory" factory-method="createInstance"/> 2 3 <bean id="personStaticFactoryWithParam" class="com.dfdc.spring.demo.PersonStaticFactory" factory-method="createInstance"> 4 <constructor-arg name="name" value="等风的草"/> 5 </bean> 6 <bean id="personStaticFactoryParams" class="com.dfdc.spring.demo.PersonStaticFactory" factory-method="createInstance"> 7 <constructor-arg name="name" value="等风的草"/> 8 <constructor-arg name="age" value="21"/> 9 </bean>
添加新bean,PersonStaticFactory.java:
1 /* 2 * 静态工厂方法实例化bean 3 */ 4 public static Person createInstance() { 5 return new Person(); 6 } 7 8 public static Person createInstance(String name) { 9 return new Person(name); 10 } 11 12 public static Person createInstance(String name,Integer age) { 13 return new Person(name,age); 14 }
测试代码:
1 ApplicationContext context = new ClassPathXmlApplicationContext(configLocation); 2 context.getBean("personStaticFactory"); 3 4 context.getBean("personStaticFactoryWithParam"); 5 6 context.getBean("personStaticFactoryWithParams");
结果:
spring调用工厂方法产生对象,但是真正创建对象还是由程序员来完成的。
spring配置文件中,只要是一个bean就会为该bean创建对象。
实例工厂实例化bean
与使用静态工厂方法实例化类似,用来进行实例化的非静态实例工厂方法位于另外一个bean中(即下面的InstanceFactory.java),容器将调用该bean的工厂方法来创建一个新的bean实例。
为使用此机制,class属性必须为空,而factory-bean 属性必须指定为当前(或其祖先)容器中包含工厂方法的bean的名称,而该工厂bean的工厂方法本身必须通过factory-method属性来设定。
添加实例工厂bean,InstanceFactory.java:
1 public class InstanceFactory { 2 3 public Person createInstance() { 4 return new Person(); 5 } 6 7 public Person createInstance(String name) { 8 return new Person(name); 9 } 10 11 public Person createInstance(String name,Integer age) { 12 return new Person(name,age); 13 } 14 }
applicationContext.xml:
1 <!--实例化工厂实例化bean --> 2 <bean id="instancefactory" class="com.dfdc.spring.demo.InstanceFactory"/> 3 4 <bean id="personInstance" factory-bean="instancefactory" factory-method="createInstance"/> 5 6 <bean id="personInstanceWithParam" factory-bean="instancefactory" factory-method="createInstance"> 7 <constructor-arg name="name" value="等风的草"/> 8 </bean> 9 <bean id="personInstanceWithParams" factory-bean="instancefactory" factory-method="createInstance"> 10 <constructor-arg name="name" value="等风的草"/> 11 <constructor-arg name="age" value="21"/> 12 </bean>
测试代码:
1 /** 2 * 实例工厂实例化bean 3 */ 4 @Test 5 public void instanceFactoryTest() { 6 ApplicationContext context = new ClassPathXmlApplicationContext(configLocation); 7 context.getBean("personInstance"); 8 }
结果:
以上。。。