Write in front
Spring helps us manage objects through the IOC container, but how to obtain these managed objects when we need one or more of them? This is related to object injection. Through injection, we can get from the spring container. Reverse passive acquisition of objects, and then we can use the capabilities of these objects to complete our tasks.
1: Setter method injection
1.1: Define the injected class
public class MyDao {
}
1.2: Define the injected class
public class MyAction {
private MyDao myDao;
public MyDao getMyDao() {
return myDao;
}
public void setMyDao(MyDao myDao) {
this.myDao = myDao;
}
}
1.3: define 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="myDao" class="yudaosourcecode.spring.MyDao"/>
<bean id="myAction" class="yudaosourcecode.spring.MyAction">
<property name="myDao" ref="myDao"/>
</bean>
</beans>
1.4: Testing
@Test
public void testDiBySetter() {
ApplicationContext ac =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
MyAction myAction = ac.getBean("myAction", MyAction.class);
System.out.println("myAction.getMyDao(): ");
System.out.println(myAction.getMyDao());
}
run:
myAction.getMyDao():
yudaosourcecode.spring.MyDao@75881071
2: Constructor injection
2.1: Define the injected class
public class DiPerson {
}
public class DiUser {
}
2.2: Define the injected class
public class ConstructorDi {
public DiPerson diPerson;
public DiUser diUser;
public ConstructorDi(DiPerson diPerson, DiUser diUser) {
this.diPerson = diPerson;
this.diUser = diUser;
}
}
Note them private DiPerson diPerson
and private DiUser diUser
we do not define read and write methods.
2.3: Define xml
<beanid="myPerson" class="yudaosourcecode.spring.DiPerson"/>
<bean id="myUser" class="yudaosourcecode.spring.DiUser"/>
<bean id="constructorDi" class="yudaosourcecode.spring.ConstructorDi">
<!--
public ConstructorDi(DiPerson diPerson, DiUser diUser) {
this.diPerson = diPerson;
this.diUser = diUser;
}
-->
<constructor-arg index="0" ref="myPerson"/>
<constructor-arg index="1" ref="myUser"/>
</bean>
2.4: Testing
@Test
public void diByConstructorTest() {
ClassPathXmlApplicationContext ac
= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ConstructorDi constructorDi = ac.getBean("constructorDi", ConstructorDi.class);
System.out.println("constructorDi.diPerson is: ");
System.out.println(constructorDi.diPerson);
System.out.println("constructorDi.diUser is: ");
System.out.println(constructorDi.diUser);
}
Output:
constructorDi.diPerson is:
yudaosourcecode.spring.DiPerson@7403c468
constructorDi.diUser is:
yudaosourcecode.spring.DiUser@43738a82
Process finished with exit code 0
3: Static factory method injection
This method is actually calling a static method of a certain class to obtain the objects needed for injection.
3.1: Define the injected class
public class DiPerson {
}
3.2: Define a static factory class
The static factory class provides static methods to return DiPerson
objects:
public class StaticFactoryCls {
public static final DiPerson generatePersonDi() {
return new DiPerson();
}
}
3.3: Define the injected class
public class StaticFactoryMethodDi {
private DiPerson diPerson;
public DiPerson getDiPerson() {
return diPerson;
}
public void setDiPerson(DiPerson diPerson) {
this.diPerson = diPerson;
}
}
3.4: define xml
<!-- 通过工厂方法定义bean -->
<bean id="staticFactoryDiPerson" class="yudaosourcecode.spring.StaticFactoryCls" factory-method="generatePersonDi"/>
<bean id="staticFactoryMethodDi" class="yudaosourcecode.spring.StaticFactoryMethodDi">
<property name="diPerson" ref="staticFactoryDiPerson"/>
</bean>
3.5: Testing
staticFactoryMethodDi.getDiPerson() is:
yudaosourcecode.spring.DiPerson@67b467e9
Process finished with exit code 0
4: Instance factory method injection
The only difference between this method and static factory method injection is that the method of generating the object is an instance method, not a static method.
4.1: Define the injected class
public class DiPerson {
}
4.2: Define the instance factory class
public class InstanceFactoryCls {
public DiPerson generatePersonDi() {
return new DiPerson();
}
}
Note that the method here is an instance method, not a static method.
4.3: Define the injected class
public class InstanceFactoryMethodDi {
private DiPerson diPerson;
public DiPerson getDiPerson() {
return diPerson;
}
public void setDiPerson(DiPerson diPerson) {
this.diPerson = diPerson;
}
}
4.4: define xml
<!-- 定义实例工厂bean -->
<bean id="instanceFactoryCls" class="yudaosourcecode.spring.InstanceFactoryCls"/>
<!-- 通过实例工厂bean获取需要DI的bean -->
<bean id="instanceFactoryDiPerson"
factory-bean="instanceFactoryCls"
factory-method="generatePersonDi"/>
<bean id="instanceFactoryMethodDi" class="yudaosourcecode.spring.InstanceFactoryMethodDi">
<property name="diPerson" ref="instanceFactoryDiPerson"/>
</bean>
4.5: Testing
@Test
public void diByInstanceFactoryTest() {
ClassPathXmlApplicationContext ac
= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
InstanceFactoryMethodDi instanceFactoryMethodDi = ac.getBean("instanceFactoryMethodDi", InstanceFactoryMethodDi.class);
System.out.println("instanceFactoryMethodDi.getDiPerson() is: ");
System.out.println(instanceFactoryMethodDi.getDiPerson());
}
run:
instanceFactoryMethodDi.getDiPerson() is:
yudaosourcecode.spring.DiPerson@5c072e3f
Process finished with exit code 0