使用@ComponentScan({"com.jiepuxun.bolipiuser","com.jiepuxun.bolipicommon"})注解扫描多个子目录时并不能生效
项目结构如下:
代码如下
package com.jiepuxun.bolipiuser;
@ComponentScan({"com.jiepuxun.bolipicommon","com.jiepuxun.bolipiuser"})
@MapperScan("com.jiepuxun.bolipidao.mapper")
@EnableCaching
@EnableMongoRepositories
@SpringBootApplication
public class BolipiUserApplication {
public static void main(String[] args) {
SpringApplication.run(BolipiUserApplication.class, args);
}
}
package com.jiepuxun.bolipicommon.mongo.repository;
@Repository
public interface MongoBlogsRepository extends MongoRepository<MongoBlogs, String> {
}
报错信息如下:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field blogsRepository in com.jiepuxun.bolipiuser.service.blog.BlogService required a bean of type 'com.jiepuxun.bolipicommon.mongo.repository.MongoBlogsRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.jiepuxun.bolipicommon.mongo.repository.MongoBlogsRepository' in your configuration.
改成下面这种方式也不生效:
@MapperScan("com.jiepuxun.bolipidao.mapper")
@EnableCaching
@EnableMongoRepositories
@SpringBootApplication(scanBasePackages = {"com.jiepuxun.bolipicommon.mongo.repository","com.jiepuxun.bolipiuser"})
我在追踪@ComponentScan注解时发现并不是注解不生效,只是interface接口不能注入而已,因此推测是我的mongodb使用方式有错误,可能是因为此时mongo还没有实例化,只是个接口,所以无法注入。
希望大家指出我的错误