编程知识 cdmana.com

Dry goods sharing: how many ways does spring Di rely on injection?

Spring Instantiation bean The way

  • Set Inject
  • Constructor Injection
  • Static factory injection
  • Example chemical plant injection

Case practice

Set Inject

xml To configure ( meanwhile spring For basic data types, the set Injection mode )

<?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.or...d">

<bean id="userDao" class="com.xxx.demo.UserDao"></bean>
<!-- setter Inject -->
<bean id="userService" class="com.xxx.demo.UserService">
<!--ref It's about the outside bean Object reference , With the quoted bean Object's id bring into correspondence with -->
<property name="userDao" ref="userDao"></property>
</bean>
</beans>

UserDao.java

public class UserDao {
public String userLogin() {
return " I am a UserDao Medium userLogin() Methods ";
}
}

UserService.java

public class UserService {
// Be sure to provide properties of setter Method
private UserDao userDao;

public void userlogin() {
String res=userDao.userLogin();
System.out.println(res);
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

App.java

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
UserService userService=applicationContext.getBean("userService", UserService.class);
userService.userlogin();
}
}

Constructor Injection

xml To configure ( Also available for basic data types 、 String equivalent Injection )

<?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.or...d">

<bean id="userDao" class="com.xxx.demo.UserDao"></bean>
<!-- Constructor Injection -->
<bean id="userServiceV2" class="com.xxx.demo.UserServiceV2">
<constructor-arg index="0" ref="userDao"></constructor-arg>
<constructor-arg index="1" value=" Three brothers of India "></constructor-arg>
</bean>
</beans>

There are three forms of constructor Injection :

index Property is parameter order , If there is only one parameter index You can leave it blank .

name Attributes are based on the name of the attribute in the constructor .

type attribute , Matches are based on the type of attribute in the constructor . If the properties of the same type are not unique , The injected attributes are injected in order .

UserServiceV2.java Class provides constructors

/**

  • Implement constructor Injection
  • @author Best Liu

*
*/
public class UserServiceV2 {
private UserDao userDao;
private String name;
public void userlogin() {
String res=userDao.userLogin();
System.out.println(res);
System.out.println(name);
}
public UserServiceV2(UserDao userDao,String name) {
super();
this.userDao = userDao;
this.name = name;
}
}

Static factory injection

xml To configure

<?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.or...d">
<!-- Static factory injection -->
<bean id="userDao01" class="com.xxx.demo.StaticFactory" factory-method="createuserDao"></bean>
<bean id="userService01" class="com.xxx.demo.UserService">
<property name="userDao" ref="userDao01"></property>
</bean>
</beans>

StaticFactory.java

public class StaticFactory {
public static UserDao createuserDao(){
return new UserDao();
}
}

UserService.java

public class UserService {
private UserDao userDao;

public void userlogin() {
String res=userDao.userLogin();
System.out.println(res);
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}

tips: Static factory injection is IoC Static factory and DI Of setter Inject , The attribute object to be injected is created by static factory .

2.4 Example chemical plant

xml To configure

<?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.or...d">

<!-- Example chemical plant -->
<bean id="instanceFactory" class="com.xxx.demo.InstanceFactory"></bean>
<bean id="userDao3" factory-bean="instanceFactory" factory-method="createUserDao"></bean>
<bean id="userService02" class="com.xxx.demo.UserService">
<property name="userDao" ref="userDao3"></property>
</bean>
</beans>

InstanceFactory.java

public class InstanceFactory {
public UserDao createUserDao(){
return new UserDao();
}
}

tips: Focus on mastering set, Constructor Injection , We can understand the factory way , In actual development, the basic use of set Way into bean.

Expand

The problem of circular dependency arises

Bean Injection by constructor , Depending on each other leads to bean Cannot instantiate .

The choice of Injection : Under development set Mode injection is preferred

Using construction injection, the dependency relationship can be established at the same time as the object is built , As soon as the object is created, everything is ready , But if you want to establish a lot of object relationships , Using construct injection leaves a long string of arguments on the build function , And it's not easy to remember , Use this time Set Injection would be a good choice .

Use Set The injection can have an explicit name , You can see what the injected object will be , image setxxx() Such a name is better than memory Constructor The position of a parameter on represents an object better .

xml To configure

<?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.or...d">
<bean id="goodsService" class="com.xxx.demo.GoodsService">
<!-- <constructor-arg index="0" ref="userService"></constructor-arg> -->
<property name="userService" ref="userService"></property>
</bean>
<bean id="userService" class="com.xxx.demo.UserService">
<!-- <constructor-arg index="0" ref="goodsService"></constructor-arg> -->
<property name="goodsService" ref="goodsService"></property>
</bean>
</beans>

GoodsService.java

public class GoodsService {
private UserService userService;
/*public GoodsService(UserService userService) {
super();
this.userService = userService;
}*/
public void setUserService(UserService userService) {
this.userService = userService;
}
}

UserService.java

public class UserService {
private GoodsService goodsService;
/* public UserService(GoodsService goodsService) {
super();
this.goodsService = goodsService;
}
*/
public void setGoodsService(GoodsService goodsService) {
this.goodsService = goodsService;
}
}

版权声明
本文为[Little Tinker who loves programming]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224152603243J.html

Scroll to Top