关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
2018-01-22 20:09
811 查看
搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考
根据英文的提示是在配置中找不到一个指定自动注入类型的bean,经过多方排查得出结论:
正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上
这个注解其实相当于下面这一堆注解的效果,其中一个注解就是@Component,在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动注册为Bean的@Service@Controller和@ Repository
至此,得出两种解决办法:
1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法
2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解,见下图
通过这两种方式,那个找不到指定Bean的错误就成功解决了
参考:http://blog.csdn.net/a532672728/article/details/77702772
Description: Field helloService in com.example.demo.service.TestController required a bean of type 'com.example.service.HelloService' that could not be found. Action: Consider defining a bean of type 'XXX' in your configuration.
根据英文的提示是在配置中找不到一个指定自动注入类型的bean,经过多方排查得出结论:
正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上
这个注解其实相当于下面这一堆注解的效果,其中一个注解就是@Component,在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动注册为Bean的@Service@Controller和@ Repository
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters={@Filter(type=CUSTOM, classes={TypeExcludeFilter.class}), @Filter(type=CUSTOM, classes={AutoConfigurationExcludeFilter.class})}) @Target(value={TYPE}) @Retention(value=RUNTIME) @Documented @Inherited
至此,得出两种解决办法:
1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法
2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解,见下图
通过这两种方式,那个找不到指定Bean的错误就成功解决了
参考:http://blog.csdn.net/a532672728/article/details/77702772
相关文章推荐
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案 标签: spring 201
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
- 关于spring boot自动注入Consider defining a bean of type 'X' in your configuration
- 关于spring boot无法自动注入bean问题解决方案
- Java问题解决:springboot启动出现-Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
- 解决spring 自动扫描包的时候出现 service无法自动注入的问题
- 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案
- 如何解决SpringBoot JpaRepository @Autowired 没法自动注入的问题
- Spring Boot之No session repository could be auto-configured, check your configuration问题解决
- 关于Spring boot中读取属性配置文件出现中文乱码的问题的解决
- 关于SpringBoot bean无法注入的问题(与文件包位置有关)改变自动扫描的包
- 关于 设备节点自动创建时 编译出现的问题 解决方法
- struts2与spring集成时,关于class属性及成员bean自动注入的问题
- struts2与spring集成时,关于Action类中成员bean自动注入的问题
- Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题
- 解决Spring+Quartz无法自动注入bean问题
- struts2与spring集成时,关于class属性及成员bean自动注入的问题
- shiro+spring boot+mybatis启动循环引用问题解决思路和方案
- CocoaPods did not set the base configuration of your project 问题解决方案