spring静态工厂注入和实例工厂注入

1、静态工厂DependencyInjectByStaticFactory类:

package com.spring.factory;
import com.spring.service.HelloApi;
import com.spring.service.impl.HelloApiImpl1;
//静态工厂类
public class DependencyInjectByStaticFactory {
public static HelloApi newInstance(String message,int index){
	return  new HelloApiImpl1(message,index);
}
}

 

2、静态工厂Bean  spring-staticFactoryDependencyInject.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="byIndex" class="com.spring.factory.DependencyInjectByStaticFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello World!"/>
<constructor-arg index="1" value="1"/>
</bean>
<bean id="byType" class="com.spring.factory.DependencyInjectByStaticFactory" factory-method="newInstance">
<constructor-arg type="java.lang.String" value="Hello World!"/>
<constructor-arg type="int" value="2"/>
</bean>
<bean id="byName" class="com.spring.factory.DependencyInjectByStaticFactory" factory-method="newInstance">
<constructor-arg name="message" value="Hello World!"/>
<constructor-arg name="index" value="3"/>
</bean>
</beans>

3、创建测试DependencyInjectByStaticFactoryTest类: 

package com.spring.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.HelloApi;
public class DependencyInjectByStaticFactoryTest {
@Test
public void DIByStaticFactory(){
	BeanFactory beanFactory=new ClassPathXmlApplicationContext("spring-staticFactoryDependencyInject.xml");
	//获取根据参数索引依赖注入的Bean
			HelloApi byIndex=beanFactory.getBean("byIndex", HelloApi.class);
			byIndex.sayHello();
			//获取根据参数类型注入的Bean
			HelloApi byType=beanFactory.getBean("byType", HelloApi.class);
			byType.sayHello();
			//获取根据参数名字依赖注入的Bean
			HelloApi byName=beanFactory.getBean("byName", HelloApi.class);
			byName.sayHello();
}
}

 4、输出结果:

1: Hello World!
2: Hello World!
3: Hello World!

 

创建实例工类:

package com.spring.factory;
import com.spring.service.HelloApi;
import com.spring.service.impl.HelloApiImpl1;
//实例工厂类
public class DependencyInjectByInstanceFactory {
public HelloApi newInstance(String message,int index){
	return new HelloApiImpl1(message,index);	
}
}

 

创建Bean配置文件spring-instanceFactoryDependencyInject.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="instanceFactory" class="com.spring.factory.DependencyInjectByInstanceFactory"/>
<bean id="byIndex" factory-bean="instanceFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello World!"/>
<constructor-arg index="1" value="1"/>
</bean>
<bean id="byType" factory-bean="instanceFactory" factory-method="newInstance">
<constructor-arg type="java.lang.String" value="Hello World!"/>
<constructor-arg type="int" value="2"/>
</bean>
<bean id="byName" factory-bean="instanceFactory" factory-method="newInstance">
<constructor-arg name="message" value="Hello World!"/>
<constructor-arg name="index" value="3"/>
</bean>
</beans>

 创建测试DependencyInjectByInstanceFactoryTest类:

package com.spring.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.HelloApi;
public class DependencyInjectByInstanceFactoryTest {
@Test
public void DIByInstance(){
	BeanFactory beanFactory=new ClassPathXmlApplicationContext("spring-instanceFactoryDependencyInject.xml");
	        //获取根据参数索引依赖注入的Bean
			HelloApi byIndex=beanFactory.getBean("byIndex", HelloApi.class);
			byIndex.sayHello();
			//获取根据参数类型注入的Bean
			HelloApi byType=beanFactory.getBean("byType", HelloApi.class);
			byType.sayHello();
			//获取根据参数名字依赖注入的Bean
			HelloApi byName=beanFactory.getBean("byName", HelloApi.class);
			byName.sayHello();
}
}

 输出结果:

1: Hello World!
2: Hello World!
3: Hello World!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值