@Resource 和 @Autowired

@Autowird

@Autowird spring framework belongs, using the default type (the byType) are implanted;

For example the following codes:

@Autowired   
public IUserService userService  

The injection system will IUserService Interface

1. If this interface is only one implementation class, it will normally injected;

2. If you have multiple implementation classes, he will complain;

3. If you had not realized, it will be an error;

This problem can be limited, as the following code:

@Autowired(required = false)   
public IUserService userService  

If you can not find the time corresponding bean does not throw an error, if required = true, the time when the bean does not exist, it will throw an exception

It can be used with @Autowird @Qualifier, in which case the automatic injection strategy is transformed into the byType byName;

The following codes:

@Autowired   
@Qualifier("userServiceImpl")   
public IUserService userService;  
或者
@Autowired   
public void setUserDao(@Qualifier("userDao") UserDao userDao) {   
    this.userDao = userDao;   
}  

 This spring will be injected (corresponding to the query based on the bean ID) in accordance with the specified id @Qualifier

 Vernacular interpretation, @ Autowired automatic annotation, give you an example, a class, a class that implements both, Autowired not know which implementation class injection and Resource have a name attribute can be distinguished.

@Resource

@Resource is JavaEE carrying annotations, based on implanting ID;

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

@Resource has two attributes are more important, points that 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. So 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.

@Resource assembly sequence
1. If both 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 a name is specified, from the context, find the name (id) matching bean assembled, can not find an exception is thrown
3. If the type is specified, the only bean from the context that matches the type found assembled, can not find or find more, will throw an exception
4. If not both Specifies the name, and no specified type, according to the automatic assembly byName manner; if there is no match, then the match is a backoff primitive type.

 

One example of a service Interface (PersonService), two serviceImpl implementation class (PersonServiceImpl1, PersonServiceImpl2), a write controller layer after implanting call:

@Controller
public class PersonController {
    @Autowired
    @Qualifier("personServiceImpl1")
    private PersonService service;
    
    @Resource(name="personServiceImpl2")
    private PersonService service2;
}

 

Published 21 original articles · won praise 0 · Views 2270

Guess you like

Origin blog.csdn.net/hfaflanf/article/details/102482429