Spring中@ResponseBody,@Autowired和@Resource

@responsebody indicates that the return result of this method is directly written into the HTTP response body , which is
generally used when acquiring data asynchronously . After using @RequestMapping , the return value is usually parsed as a jump path, and the return result will not be parsed after adding @responsebody For the jump path, it is directly written into the HTTP response body. For example, asynchronously obtaining json data and adding @responsebody will directly return json data. (Commonly used in the interface method that provides data to the app), the network data obtained by the app is usually obtained asynchronously.

 

The @Autowired annotation is to assemble dependent objects by type. By default, it requires that the dependent object must exist. If null values ​​are allowed, you can set its required property to false. If we want to use assembly by name, we can use it in conjunction with the @Qualifier annotation. as follows:

 

@Autowired  @Qualifier("userDao")

private PersonDao  personDao;
  @Resource is automatically injected by name by default, provided by J2EE.

    Need to import Package:   javax.annotation.Resource

    @Resource has two important attributes: name and type, and Spring resolves the name attribute of the @Resource annotation to the bean's

    name, and the type attribute resolves to the type of the bean. So if the name attribute is used, the automatic injection strategy of byName is used, and the

    When the type attribute is used, the byType automatic injection strategy is used. If neither the name nor the type attribute is specified, the strategy will be automatically injected by name using the reflection mechanism .

@Resource(name=“userDao”)

private UserDao userDao;//For the field

@Resource(name=“userDao”)

public void setUserDao(UserDao userDao) {//On the setter method of the property

     this.userDao= userDao;

}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326915847&siteId=291194637