The sum of the difference @Autowired spring assembly @Resource annotations

@Autowired

Notes @Autowired to Spring offers, you need to import the package org.springframework.beans.factory.annotation.Autowired. @Autowired strategy adopted is in accordance with the type of injection.

@Autowired default by type fitting (This comment is of spring metal industry), default claim dependent objects must be present, if you want to allow null values, can set its required property is false, such as: @Autowired (required = false) If we want to use the name of the assembly can be combined @Qualifier annotation use.

public class UserService {
    @Autowired
    private UserDao userDao; 
}

As shown in the code, so that spring back to the container assembly of the type found in UserDao class, then injected in. This creates a problem when a type of bean has more value, will result in a situation which can not select specific injection, this time we need to meet the @Qualifier use.

That is, when a plurality of the interface implementation classes, such as a plurality of interfaces UserDao implementation classes, if the press types match, the IoC container does not know which specific implementation class requires injection.

@Qualifier tell which objects specific to spring assembly.

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

This time we will be able to locate the object we want to inject by type and name.

@Resource

@Resource annotation provided by J2EE, need to import the package javax.annotation.Resource. @Resource default automatically injected in accordance with ByName.

@Resource effect equivalent to @Autowired, but @Autowired automatically injected by byType, while @Resource default automatically injected by byName.

@Resource has two attributes are more important, are the name and type, Spring will @Resource annotation name attribute resolves to the bean's name, the type attribute is resolved to the type of bean.

Therefore, if you use the name attribute is used byName automatic injection strategy, and is used when using the type attribute byType automatic injection strategy. If neither type attribute name is assigned, the automatic injection strategy case byName by using reflection.

Annotations @Resource assembly sequence of:

  1. If you specify the name and type, from the Spring context to find a unique match of bean assembled, can not find an exception is thrown;
  2. If the name is specified, from the context, find the name (id) match bean assembled, can not find an exception is thrown;
  3. If the type is specified, from the context that matches the type of bean you find a unique assembled, can not find or find more, will throw an exception;
  4. If neither name, but do not specify a type, then automatically assembled according to byName way; if there is no match, then rolled back to a primitive type to match, if the match is successful, the automatic assembly.

Spring belonging to a third party, J2EE Java is its own thing. Use @Resource can reduce the coupling between the code and Spring.

 

And together with the general @Autowired @Qualifier, @ Resource alone.

 

Of course, there is no conflict, then @Autowired can also be used separately

 

Guess you like

Origin www.cnblogs.com/kjitboy/p/12122822.html