Spring用静态工厂和实例工厂的方式注入

一、静态工厂方法注入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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值