The difference between @Autowired and @Resource

The difference between @Autowired and @Resource

1. @Resource annotation

If @Resource does not specify name and type, the byName injection strategy will be used first. If there is no match, the byType strategy will be used. If neither succeeds, an error will be reported.

public class UserAction {
    
    
//    @Autowired
    @Resource(type = UserService.class)
    private UserService userService400;//如果按照类型,要求容器中只能有一个对象
    public void sayOk(){
    
    
        System.out.println("UserService="+userService400);
        userService400.hi();
    }

    public void setUserService(UserService userService) {
    
    
        this.userService400 = userService;
    }
}

Error example:

    <bean class="com.linghu.service.UserService" id="userService100"/>
    <bean class="com.linghu.service.UserService" id="userService200"/>
    <bean class="com.linghu.service.UserService" id="userService300"/>

Reason for the error: Here @Resource(type = UserService.class)means that this annotation is automatically assembled and injected by type . The type is UserServicean object type. Scan the bean. If you find a duplicate UserService bean type , the injection will fail~

Correct demonstration:

    <bean class="com.linghu.service.UserService" id="userService100"/>//容器只有一个对象
<!--    <bean class="com.linghu.service.UserService" id="userService200"/>-->
<!--    <bean class="com.linghu.service.UserService" id="userService300"/>-->

If according to the name attribute:

public class UserAction {
    
    
//    @Autowired
    @Resource(name = "userService01")//去容器里找name相同的bean对象
    private UserService userService400;//
    public void sayOk(){
    
    
        System.out.println("UserService="+userService400);
        userService400.hi();
    }

    public void setUserService(UserService userService) {
    
    
        this.userService400 = userService;
    }
}

Correct demonstration:

   <bean class="com.linghu.service.UserService" id="userService100"name="userService01"/>
public class Springtest {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext gt =
                new ClassPathXmlApplicationContext("beans03.xml");
        Object userService100 = gt.getBean("userService100");//通过类型获取bean对象
        System.out.println("UserService="+us);
    }
}

2. @Autowired annotation

@Controller("userAction01")//bean 的 id=userAction01
public class UserAction {
    
    

    @Autowired
    private UserService userService;//将Userservice对象注入到UserAction中,可以调用UserService的方法了。相当于在这个类new了一个UserService类的对象。
    public void sayOk(){
    
    
        System.out.println("UserService="+userService);
        userService.hi();
    }


}

The Bean file configuration is as follows: add two automatic scans, scan two sub-packages componentand servicethe component classes under them. There is no need to set the bean object, because the bean object has been replaced by the above annotation.

  <context:component-scan base-package="com.linghu.component"/>
  <context:component-scan base-package="com.linghu.service"/>
public class Springtest {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext gt =
                new ClassPathXmlApplicationContext("beans03.xml");
        UserAction userAction = (UserAction) gt.getBean(UserAction.class);//获取对象
      //UserAction userAction = (UserAction) gt.getBean("userAction01");//也可以这样获取对象
        System.out.println(userAction);
        userAction.sayOk();
    }
}

Guess you like

Origin blog.csdn.net/weixin_43891901/article/details/130203734