@Resource和@Autowired

1,165 阅读1分钟

www.cnblogs.com/think-in-ja…

blog.csdn.net/magi1201/ar…

@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。

Demo:

public interface Human {

    String runMarathon();
}

@Service
public class Man implements Human {
    @Override
    public String runMarathon() {
        return "man ---";
    }
}
@Service
public class Woman implements Human {
    @Override
    public String runMarathon() {
        return "woman ---";
    }
}

@Service
public class TestMan {

    public String runMarathon() {
        return "testman ---";
    }
}

使用@Resource

case1:

@RestController
@RequestMapping("/hello")
public class HelloController {
	
    @Resource
    private Human testman;
    
    @GetMapping("/uu")
    public String list() {
        return testman.runMarathon();
    }
}

启动spring boot直接报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.imooc.luckymoney.Human' available: expected single matching bean but found 2: man,woman
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcess

case2:

@RestController
@RequestMapping("/hello")
public class HelloController {
	
    @Resource
    private Human human;
    
    @GetMapping("/uu")
    public String list() {
        return human.runMarathon();
    }
}

启动spring boot直接报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.imooc.luckymoney.Human' available: expected single matching bean but found 2: man,woman
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:324) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]

case3:

@RestController
@RequestMapping("/hello")
public class HelloController {
	
    @Resource
    private Human man;
    
    @GetMapping("/uu")
    public String list() {
        return man.runMarathon();
    }
}

输出:man--

case4:

@RestController
@RequestMapping("/hello")
public class HelloController {
	
    @Resource
    private Human man;
    
    @GetMapping("/uu")
    public String list() {
        return man.runMarathon();
    }
}

输出:woman--

使用@Autowired

case1:

编译直接报错

case2:

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private Human man;

    @GetMapping("/uu")
    public String list() {
        return man.runMarathon();
    }
}

输出:man--

case3:

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private Human woman;

    @GetMapping("/uu")
    public String list() {
        return woman.runMarathon();
    }
}

输出:woman--

case4:

编译器直接报错