annotation injection and difference of Spring @Resource, @Autowired, @Qualifier

Source: Internet
Author: User

spring2.5 provides an annotation (annotation-based) based configuration, and we can do this by annotating the injection dependencies. In Java code, you can use @Resource or @autowired annotations to inject rows. Although @resource and @autowired can be used to complete injection dependencies, there is a difference between them. First Look at:

A. @Resource by default, the injection is assembled by name, and the injection is assembled according to the type only if the bean matching the name is not found;

B. @Autowired The default is injected according to the type assembly, if you want to use the name to transfer the injection, you need to combine @qualifier;

C. @Resource annotations are also provided by Java, and @autowired is provided by spring, so reducing the system's reliance on spring suggests using

The way @Resource;

D. Both @Resource and @autowired can be written on top 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 callout, 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-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

<context:annotation-config/>

</beans>

3, modify the above configuration file header information, we can in the Java code by annotating the way to inject beans, look at the following code

(1) @Resource

public class StudentService3 implements Istudentservice {

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


Private String ID;


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

@Resource (name= "Studentdao")//To assemble a field Studentdao from the spring configuration file looking for a bean named Studentdao, if the spring configuration file does not exist The bean with the Studentdao name is turned to the bean type to find the row
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 possible to put it here.
Private Istudentdao Studentdao;


Private String ID;


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

@Autowired//Through this note to find the bean that satisfies the Studentdao type from the spring configuration file

//@Qualifier ("Studentdao") to find a match by the name 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"/>

You can assemble in Java code using either @autowire or @resource annotations, the difference between the two annotations is:
@Autowire is assembled by type by default, which requires that dependent objects must exist. If allowed null, you can set it required property to False, if we want to use by name assembly, can be used in conjunction with @qualifier annotation;


@Resource by default, when a bean matching the name is not found to be assembled according to the type, can be specified by the Name property, if the Name property is not specified, and when the annotation is annotated on the field, that is, the default Fetch field is used as the bean name for the dependent object. When the annotation is annotated on the property's setter method, the default Fetch property name is used as the bean name to find the dependent object.

Note: If the Name property is not specified, and the dependent object is still not found by the default name, it will fall back to assembly by type, but once the name attribute 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.