Three ways of Java dependency injection

Spring implements IOC (inversion of control) through DI (dependency injection). There are three common injection methods: construction method injection, setter injection and annotation based injection.

Construction method injection
Constructor injection: ensures that some necessary properties are set when the bean is instantiated, and that the bean instance can be used after instantiation

1. In the class, you don't need to set the setter method for the property, just provide the constructor
2. Configure the class bean in the construction file, and configure the constructor. In the configuration constructor, use the

//ApplicationContext.xml
<bean id="action" class="com.action.UserAction">
    <constructor-arg index ="0" name="name" value="Murphy"></constructor-arg>
</bean>

Provide construction method

public class UserAction {
 private  String name;
   public UserAction(String name) {
         this.name = name;
    }
}

  

setter injection
1. Find the corresponding setter method according to the value of the name property of the property tag
For example: name = "aa" corresponds to setAa method
2. Attribute injection is the most commonly used method because of its high selectivity and flexibility
3. Property injection requires the bean to provide a default constructor, and provide the corresponding setter method for the property to be injected. spring first calls the bean default constructor to instantiate the bean object, and then calls the setter method to inject the property value through the method of the reflection mechanism
4. One more thing to note: if you inject attributes through the set method, spring will instantiate the object through the default empty parameter construction method. If you write a construction method with parameters in the class, you must write the construction method with empty parameters. Otherwise, spring cannot instantiate the object, resulting in an error.

 //ApplicationContext.xml
<bean id="action" class="com.action.UserAction">
       <property name="name" value="Murphy"/>
</bean>

Provide setting method

public class UserAction {
      private String name;
   public String getName() {
        return name;
    }   
   public void setName(String name) {
        this.name = name;
    } 
 }

  

Annotation injection
@ Autowired (construction, interface, method)
Auto assembly, default injection by type
- property Required

1.@Autowired(required=true): when @ Autowired annotation is used, the default is @ Autowired(required=true). It means that the bean must exist when injecting, or the injection will fail
2.@Autowired(required=false): it means that the bean to be injected is ignored. If there is direct injection, if it is not skipped, no error will be reported.
      3.required The meaning of the attribute is the same as @ Required, except that @ Required is only applicable to the setter injection mode based on XML configuration and can only be typed on the setting method.

public class AutowiredAction {

	private String name;
	private List<String> list;
	
	@Autowired
	private AutowiredAction(String name) {
		this.name=name;
	}
	
	public String getName() {
		return name;
	}

	@Autowired
	public void setName(String name) {
		this.name = name;
	}
	
	@Autowired(required = true)
	private void initName(String name,List<String> list) {
		this.name = name;  
        this.list = list;
	}

}

Interface

public interface AutowiredIn {

	@Autowired
	 void initName(String name,Integer age);
}

@ Resource is assembled by name by default
It can be labeled on the setter method of a field or property. By default, go to the Spring container to find the dependent object according to the name of the field. If not, return to find by type
If the property name is configured
Then you can only find dependent objects by name

public class ResourceAction {

	@Resource(name="name")
	private String name;
	
	@Resource
	private List<String> list;
	
	
	public String getName() {
		return name;
	}
	@Resource
	public void setName(String name) {
		this.name = name;
	}
	
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	
}

Tags: Java Spring xml Attribute

Posted on Thu, 14 May 2020 05:49:15 -0400 by woodplease