一、静态工厂方法注入bean
调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节。
在XML文件中配置bean时,要声明通过静态方法创建的 bean , 需要在 bean 的 class 属性里面指定拥有该工厂的方法的类 , 同时在 factory-method 属性里指定工厂方法的名称。最后 , 使用 元素为该方法传递方法参数。
实例如下:
package com.entity;
public class Card {
private String cno;
private String cpwd;
public Card() {
super();
// TODO Auto-generated constructor stub
}
public Card(String cno, String cpwd) {
super();
this.cno = cno;
this.cpwd = cpwd;
}
public String getCno() {
return cno;
}
public void setCno(String cno) {
this.cno = cno;
}
public String getCpwd() {
return cpwd;
}
public void setCpwd(String cpwd) {
this.cpwd = cpwd;
}
@Override
public String toString() {
return "Card [cno=" + cno + ", cpwd=" + cpwd + "]";
}
}
2、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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- 静态工厂 -->
<!--class要写工厂的类;factory-method要写工厂类中用于创建bean的方法-->
<bean id="cardStatic" class="com.util.StaticFactory"
factory-method="getCar">
<constructor-arg value="1" />
</bean>
</beans>
3、测试:
package com.temp;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.entity.Card;
import com.entity.Person;
public class SpringTemp {
@Test
public void createBean() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Card card=(Card) applicationContext.getBean("cardStatic");
System.out.println(card);
}
}
4、输出的结果:
Card [cno=1, cpwd=admin1]
二、实例化静态工厂注入bean
实例工厂方法:将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时 , 只需要简单的调用该实例方法而不需要关心对象的创建细节。
要声明通过实例工厂方法创建的 bean:
1.在 bean 的factory-bean 属性里指定拥有该工厂方法的bean。
2.在 factory-method 属性里指定该工厂方法的名称。
3.使用 元素为工厂方法传递方法参数。
实例如下:
1.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- 实例工厂 -->
<!--首先创建一个工厂的bean-->
<bean id="cardFactory" class="com.util.InstanceFactory"></bean>
<!--factory-bean指定前面已经创建的bean; factory-method指定工厂实例中用于创建car的方法; constructor-arg指定创建car方法中传入的参数-->
<bean id="cardInstance" factory-bean="cardFactory"
factory-method="getCar">
<constructor-arg value="2" />
</bean>
</beans>
2.测试:
package com.temp;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import com.entity.Card;
import com.entity.Person;
public class SpringTemp {
@Test
public void createBean() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Card card2=(Card) applicationContext.getBean("cardInstance");
System.out.println(card2);
}
}
3、输出结果
Card [cno=2, cpwd=admin2]