Spring Boot 异常集锦

没有加载 XX service

问题
Consider defining a bean of type 'org.spring.springboot.service.CityService' in your configuration.
解决:

  1. 由很多原因造成的这个异常,常见的是 启动类即Application 放的位置不对,解决办法是使用一个注解:
@SpringBootApplication
@ComponentScan(basePackages = {"org.spring.springboot.service", "org.spring.springboot.service.impl"})
@MapperScan("org.spring.springboot.dao")
public class SpringbootApplication{

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}
  1. 但是我犯的错误相当低级,我的 service 是个接口,在我实现该接口时丢了关键字implements(也就是说我只定义了接口但没实现接口),我实现了接口就正确了。
@Service
public class CityServiceImpl implements CityService{

    @Autowired
    private CityDao cityDao;

    public City findCityByName(String cityName){
        return cityDao.findByName(cityName);
    }
}

XXX Service 没有被 autowire

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.zzjack.wxorder.dao.
ProductInfoDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:

{@org.springframework.beans.factory.annotation.Autowired(required=true)}

原因使用 Mybatis, 但是没有在启动类那里注册。加上@MapperScan 这个注解,指明 dao 层所在的位置。

@SpringBootApplication
@MapperScan("com.zzjack.wxorder.dao")
public class WxorderApplication {

    public static void main(String[] args) {
        SpringApplication.run(WxorderApplication.class, args);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容