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