Spring4共提供了三种实例化bean的方式,分别为构造器实例化、静态工厂实例化、动态方法实例化,
下面一一详解:文章出自这里!
公用使用的实体
package com.gditc.test.domain;
/**
* 测试实体
*
* @author JiangI
*/
public class Person {
private String name;
private Integer age;
public Person() {
super();
System.out.println("无参构造器");
}
public Person(String name) {
super();
this.name = name;
System.out.println("带name参数的构造器:" + name);
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
System.out.println("带name及age参数的构造器" + name + ":" + age);
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
方式一:
xml配置
<!-- 实例化方式一:构造器 -->
<!-- 无参构造器实例化 -->
<bean id="personNo" class="com.gditc.test.domain.Person"/>
<!-- 带name参数的构造器实例化 -->
<bean id="personWithName" class="com.gditc.test.domain.Person">
<constructor-arg name="name" value="username1"/>
</bean>
<!-- 带name及age参数的构造器实例化 -->
<bean id="personWithNameAndAge" class="com.gditc.test.domain.Person">
<constructor-arg name="name" value="username2"/>
<constructor-arg name="age" value="22"/><!-- 注意这里的value一定要和Person类属性的age的类型一致,否则报错 -->
</bean>
测试
/**
* 无参构造器 正确运行条件:有一个无参构造器,否则报错(无参构造器必须显示写出来)
*/
@Test
public void testPersonNo() {
Resource resource = new ClassPathResource("beans-instance.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Person person = (Person) beanFactory.getBean("personNo");
System.out.println(person);
}
/*
* 带name参数构造器
*/
@Test
public void testPersonWithName() {
Resource resource = new ClassPathResource("beans-instance.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Person person = (Person) beanFactory.getBean("personWithName");
System.out.println(person);
}
/*
* 带name及age参数构造器
*/
@Test
public void testPersonWithNameAndAge() {
Resource resource = new ClassPathResource("beans-instance.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Person person = (Person) beanFactory.getBean("personWithNameAndAge");
<span style="white-space:pre"> </span>System.out.println(person);
}
以上是构造器实例化方式, 这个方法比较实用,必须掌握!!!
方式二:静态工厂
静态工厂实例化方法需要另外建立一个工厂类
package com.gditc.test.domain;
/**
* 静态工厂实例化
* @author JiangI
*
*/
public class PersonStaticFactory {
<span style="white-space:pre"> </span>//无参
public static Person createPersonInstance() {
return new Person();
}
//有参
public static Person createPersonInstance(String name) {
return new Person(name);
}
}
本工厂类提供两个方法,分别对应带参数、无参构造器。
xml配置
<!-- 实例化方式二:静态工厂 -->
<!-- 无参数工厂创建方法 -->
<bean id="personStaticFactory" class="com.gditc.test.domain.PersonStaticFactory" factory-method="createPersonInstance"/>
<!-- 带一参数工厂创建方法 -->
<bean id="personStaticFactoryWithName" class="com.gditc.test.domain.PersonStaticFactory" factory-method="createPersonInstance">
<span style="white-space:pre"> </span><constructor-arg name="name" value="username3"/>
</bean>
测试
/*
* 无参静态工厂方法构造 (无论工厂方法是什么修饰符都可以)
*/
@Test
public void testStaticFacotyNo() {
Resource resource = new ClassPathResource("beans-instance.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Person person = (Person) beanFactory.getBean("personDynamicFactory");
System.out.println(person);
}
/*
*带name参数工厂方法构造
*/
@Test
public void testStaticFacotyWithName() {
Resource resource = new ClassPathResource("beans-instance.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Person person = (Person) beanFactory.getBean("personDynamicFactoryWithName");
System.out.println(person);
}
方式三:动态工厂
与静态工厂大同小异,动态工厂的方法为非静态
package com.gditc.test.domain;
/**
* 动态工厂
* @author JiangI
*
*/
public class PersonDynamicFactory {
//无参
public Person dynamicCreatePerson() {
return new Person();
}
//有参
public Person dynamicCreatePersonWithName(String name) {
return new Person(name);
}
}
<!-- 实例化方式三:动态工厂 -->
<!-- 无参数工厂创建方法 -->
<bean id="personDynamicFactory" class="com.gditc.test.domain.PersonDynamicFactory" factory-method="dynamicCreatePerson"/>
<!-- 带一参数工厂创建方法 -->
<bean id="personDynamicFactoryWithName" class="com.gditc.test.domain.PersonDynamicFactory" factory-method="dynamicCreatePersonWithName">
<constructor-arg name="name" value="username4"/>
</bean>
测试
/*
* 无参动态工厂方法构造(无论工厂方法是什么修饰符都可以)
*/
@Test
public void testDynamicFacoty() {
Resource resource = new ClassPathResource("beans-instance.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Person person = (Person) beanFactory.getBean("personDynamicFactory");
System.out.println(person);
}
/*
* 带name参数的工厂构造(无论工厂方法是什么修饰符都可以)
*/
@Test
public void testDynamicFacotyWithName() {
Resource resource = new ClassPathResource("beans-instance.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
Person person = (Person) beanFactory.getBean("personDynamicFactoryWithName");
System.out.println(person);
}
动态工厂实例化的方式出现错误,暂时找不到原因