background

Today, I saw a big truck on my way home from work, and I thought of assembly. Then I thought of @autowired, and then I thought of the @resource annotation used for assembly of recent work projects. So the spirit of learning what things to drill in the end to be able to change from the novice spirit of god!! I took a hard look at it and summed it up here. The following contents first explain the two notes respectively, and then summarize the similarities and differences.

The @autowired @autowired annotation for the Spring, you need to import the package org. Springframework. Beans. Factory. The annotation. Autowired.

The strategy adopted by @Autowired is injection by type.

public class UserService {
    @Autowired
    private UserDao userDao; 
}
Copy the code

As shown in the code above, this assembly goes back to the Spring container and finds a class of type UserDao and injects it. The problem with this is that when a type has multiple bean values, it is impossible to select which one to inject, and we need to use it with @Qualifier.

The @qualifier tells Spring exactly which object to assemble.

public class UserService {
    @Autowired
    @Qualifier(name="userDao1")    
    private UserDao userDao; 
}
Copy the code

At this point we can locate the object we want to inject by type and name.

@Resource

@ the Resource annotations provided by J2EE, you need to import the package javax.mail. The annotation. The Resource.

@Resource is automatically injected ByName by default.

public class UserService {
    @Resource  
    private UserDao userDao; 
    @Resource(name="studentDao")  
    private StudentDao studentDao; 
    @Resource(type="TeacherDao")  
    private TeacherDao teacherDao; 
    @Resource(name="manDao".type="ManDao")  
    private ManDao manDao; 
}    
Copy the code

If both name and type are specified, a unique matching bean is found in the Spring context for assembly. If not, an exception is thrown.

(2) If name is specified, look for the bean whose name (ID) matches from the context for assembly, and throw an exception if not found.

(3) If type is specified, an exception will be thrown if a unique bean with a similar match is found in the context.

④ If neither name nor type is specified, the assembly is automatically carried out in byName mode. If there is no match, the match is rolled back to a primitive type, and if there is a match, auto-assembly is performed.

conclusion

Spring is third-party; J2EE is Java’s own thing. Using @Resource reduces the coupling between your code and Spring.

Both can be written in fields and setter methods. If you write both on the field, then you don’t need to write the setter.

When multiple types exist, but no type is specified, the following error is reported:

Severe: Exception sendingcontext initialized event to listener instance of classorg.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating beanwith name'sequenceServiceImpl': Injection of resource dependencies failed; nested exception isorg.springframework.beans.factory.NoUniqueBeanDefinitionException: Noqualifying bean oftype 
Copy the code