annotation injection and difference of Spring Resource, autowired and qualifier

Source: Internet
Author: User
 Description and Difference

spring4.1 provides annotations (annotation-based)-based configuration, which we can use to complete injection dependencies. In Java code, you can use @Resource or @autowired annotations to line-inject. Although both @resource and @autowired can be used to complete the injection dependency, there is a difference between them. First Look at:

A. @Resource By default is assembled by name, only if the bean that matches the name is not found to assemble the injection according to the type;

B. @Autowired By default is injected by type assembly, if you want to transfer the injection by name, it needs to be used in conjunction with @qualifier;

C. @Resource annotations are provided by the Java EE, and @autowired is provided by spring, thus reducing the system's reliance on spring to recommend use

The way @Resource;

is a class in the Annotation-api.jar package, Tomcat already contains this jar package and does not need to import it yourself. If not, then manually import the package.

D. @Resource and @autowired can be written as an example of a field or setter method for that field

2, the use of annotations, we need to modify the spring configuration file header information, modify some of the red label, as follows

<?xml version= "1.0" encoding= "UTF-8"?>
< beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-4.1.xsd ">

<context:annotation-config/>

</beans>

3, after modifying the header information of the above configuration file, we can inject the bean in the Java code through annotations, see the following code

(1) @Resource

public class StudentService3 implements Istudentservice {

//@Resource (name= "Studentdao") is also possible here
Private Istudentdao Studentdao;


Private String ID;


public void SetId (String id) {
This.id = ID;
}

@Resource (name= "Studentdao")//Use this note to find a bean with the name Studentdao from the spring configuration file to assemble the field Studentdao if it does not exist in the spring configuration file The bean with the Studentdao name is shifted to the bean type by line lookup
public void Setstudentdao (Istudentdao Studentdao) {
This.studentdao = Studentdao;
}


public void Savestudent () {
Studentdao.savestudent ();
System.out.print (", ID:" +id);
}


}

The configuration file adds the following information

<bean id= "Studentdao" class= "Com.wch.dao.impl.StudentDao" ></bean>
< bean id= "StudentService3" class= "Com.wch.service.impl.StudentService3"/>

(2) @Autowired

public class StudentService3 implements Istudentservice {

//@Autowired It is also possible to put it here
Private Istudentdao Studentdao;


Private String ID;


public void SetId (String id) {
This.id = ID;
}

@Autowired//Use this note to find beans that satisfy the Studentdao type from the spring configuration file

//@Qualifier ("Studentdao") to find the transferred by name by line
public void Setstudentdao (Istudentdao Studentdao) {
This.studentdao = Studentdao;
}


public void Savestudent () {
Studentdao.savestudent ();
System.out.print (", ID:" +id);
}


}

The configuration file adds the following information

<bean id= "Studentdao" class= "Com.wch.dao.impl.StudentDao" ></bean>

< bean id= "StudentService3" class= "Com.wch.service.impl.StudentService3"/>

difference

The Java code can be assembled using @autowire or @resource annotations, the differences between the two annotations are:
@Autowire By default assembly by type, by default it requires that the dependent object must exist if it is allowed to be null, you can set it required property to False, if we want to use by name assembly, can be used in conjunction with @qualifier annotations;


@Resource is assembled by name by default, when no bean matching the name is found to be assembled by type, it can be specified by the Name property, if the Name property is not specified, when the annotation is labeled on the field, that is, the default fetch field names as the bean name for the dependent object. When annotations are labeled on the setter method of the property, the default property name is used as the bean name for the dependent object.

Note: If the Name property is not specified, and the dependent object is still not found by default name, it will fall back to assembly by type, but once the Name property is specified, it can only be assembled by name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.