Do you know there are several ways of Spring DI dependency injection?

For those who want more related information, please see the personal introduction on the homepage!

Spring's way of instantiating beans

  • Set injection
  • Constructor injection
  • Static factory injection
  • Instantiated factory injection

Case practice

Set injection

xml configuration (at the same time spring also provides a set injection method for basic data types)

<?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="userDao" class="com.xxx.demo.UserDao"></bean>
	<!-- setter注入 -->
	<bean id="userService" class="com.xxx.demo.UserService">
        <!--ref是对于外部bean对象引用,与被引用的bean对象的id保持一致-->
		<property name="userDao" ref="userDao"></property>
	</bean>
</beans>

UserDao.java

public class UserDao {
    
    
	public String userLogin() {
    
    
		return	"我是UserDao中的userLogin()的方法";
	}
}

UserService.java

public class UserService {
    
    
    //一定要提供属性的setter方法
	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 configuration (also provides injection of basic data types, strings, etc.)

<?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="userDao" class="com.xxx.demo.UserDao"></bean>
	<!-- 构造器注入 -->
	<bean id="userServiceV2" class="com.xxx.demo.UserServiceV2">
		<constructor-arg index="0" ref="userDao"></constructor-arg>
		<constructor-arg index="1" value="印度三哥"></constructor-arg>
	</bean>
</beans>

There are three forms of constructor injection:

The index attribute is the order of the parameters. If there is only one parameter index, you can leave it alone.

The name attribute is based on the name of the attribute in the constructor.

The type attribute is matched according to the type of the attribute in the constructor. If the attributes of the same type are not unique, the injected attributes are injected in order.

The UserServiceV2.java class provides a constructor

/**
 * 实现构造器注入
 * @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 placement

<?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="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 the setter injection of IoC static factory and DI. The property objects that need to be injected are created using the static factory.

2.4 Instantiate the factory

xml placement

<?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.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, and understanding the factory method. In actual development, the set method is basically used to inject beans.

Expand

The problem of circular dependence

Beans are injected through the constructor, and they rely on each other to cause the bean to fail to instantiate.

Injection options: the first choice for set injection in development projects

Use construction injection to complete the establishment of the dependency relationship at the same time as the object is built. Once the object is established, everything is ready. However, if there are many object relationships to be established, using construction injection will leave one on the constructor. Long string of parameters, and not easy to remember, then using Set injection would be a good choice.

Using Set injection can have a clear name, and you can understand what the injected object will be. A name like setxxx() is better than remembering the position of a parameter on the Constructor to represent a certain object.

xml placement

<?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="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;
	}	
}

Insert picture description here

Guess you like

Origin blog.csdn.net/xyx12321/article/details/111315201