没有加载 XX service
问题
Consider defining a bean of type 'org.spring.springboot.service.CityService' in your configuration.
解决:
- 由很多原因造成的这个异常,常见的是 启动类即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);
}
}
- 但是我犯的错误相当低级,我的 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);
}
}