码迷,mamicode.com
首页 > 编程语言 > 详细

Guide to Spring @Autowired

时间:2018-02-22 19:39:41      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:targe   框架   which   tor   sharp   exception   href   访问   ota   

Guide to Spring @Autowired

Spring希望由@Autowired注解的依赖在某个依赖bean被构造时是可以访问的。如果框架不能解析这个用于wiring的bean,就会抛出异常并阻止Spring容器的启动:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.autowire.sample.FooDAO] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. 
Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

要避免这种情况的发生,一个bean可以这样选择性地指定:

public class FooService {
 
    @Autowired(required = false)
    private FooDAO dataAccessor; 
     
}

 

Guide to Spring @Autowired

标签:targe   框架   which   tor   sharp   exception   href   访问   ota   

原文地址:https://www.cnblogs.com/niaomingjian/p/8459321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!