搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考
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 'com.example.service.HelloService' in your configuration.
然后我又看了下自己写的几个类以及注解见下面,感觉写的没有问题啊
控制器 TestController

接口HelloService

接口对应的实现类HelloServiceImpl

根据英文的提示是在配置中找不到一个指定自动注入类型的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的错误就成功解决了。。。。。那么这篇就这样吧
PS:控制器Controller也要放在与application同级或者子目录下,道理大致一样
搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Description:Field helloService in com.example.demo.service.TestController required a bean of type 'co
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 t...
2.位与运算符运算规则:0&0=0&1=1&0=0,1&1=1
位或运算符运算规则:0|1=1|0=1|1=1,0|0=0
位求反运算符运算规则:0=1,1=0,对于整数x有~x=-(x+1)
位异或运算符运算规则:00=11=0,01=10=1
左移位运算符运算规则:原来的所有位左移,最低位补0,相当于乘以2
右移位运算符运算规则:原来的所有位右移,最低位丢弃,最高位使用符号位填充,相当于整除2 TOC](这
Spring Boot Consider defining a bean of type `xxx` in your configuration 错误6种情况解决(Spring、Dubbo、JPA等)
SpringBoot启动报错Consider defining a bean of type ‘XXX’ in your configuration.
在业务模块集成公共模块的时候,将业务模块的domain层、mapper层、service层移动到公共模块,提高代码的复用性,易于维护和扩展。
改造好了之后,启动时却报错了。如上图。
大意是咱业务模块的controller中没有咱service层的这个bean。
自己解决的时候是先去查看包名有没有写错,位置有没有对。发现没有问题之后查看service的实现类.
Consider defining a bean of type ‘com.example.ch2_1_1.repository.TodoRepository’ in your configuration.
我写的几个类
但运行时出错spring给建议为Consider defining a bean of type ‘xxx‘ in your configuration
英文提示为在配置中找不到一个指定自动注入类型的bean,在正常情况下@Component注解的类会自动被Spr
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Sp
关于spring boot整合mybatis使用oracle数据库出现could not load:oracle.jdbc.driver.OracleDriver问题的终极解决方案
33367
关于spring boot自动注入出现Consider defining a bean of type ‘xxx‘ in your configuration问题解决方案
尴尬的水果刀:
关于spring boot自动注入出现Consider defining a bean of type ‘xxx‘ in your configuration问题解决方案
关关雎鸠ԅ(¯﹃¯ԅ):
NotePad++针对一批数据批量添加双引号和逗号以及去除空格的处理技巧
王德发!:
Java中将xml文件转化为json的两种方式
忘川一渡人:
关于spring boot自动注入出现Consider defining a bean of type ‘xxx‘ in your configuration问题解决方案
liguoping001: