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!