Clare_CPU 2022-04-14 22:40 采纳率: 100%
浏览 1601
已结题

@ComponentScan扫描多个包失效问题

使用@ComponentScan({"com.jiepuxun.bolipiuser","com.jiepuxun.bolipicommon"})注解扫描多个子目录时并不能生效

项目结构如下:

img

代码如下

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.

出现错误后,我改用@ComponentScans也不起作用,只有在@ComponentScan参数仅有一个的时候才会生效,但是这样就无法扫描到启动类所在包的controller类了,网上大多数答案都说使用@ComponentScan({"com.example.demo1","com.example.demo2"})格式的注解,我就是这么写的,但是并没有生效。

改成下面这种方式也不生效:
@MapperScan("com.jiepuxun.bolipidao.mapper")
@EnableCaching
@EnableMongoRepositories
@SpringBootApplication(scanBasePackages = {"com.jiepuxun.bolipicommon.mongo.repository","com.jiepuxun.bolipiuser"})

我在追踪@ComponentScan注解时发现并不是注解不生效,只是interface接口不能注入而已,因此推测是我的mongodb使用方式有错误,可能是因为此时mongo还没有实例化,只是个接口,所以无法注入。
希望大家指出我的错误

  • 写回答

5条回答 默认 最新

  • Clare_CPU 2022-04-15 08:56
    关注

    问题解决啦,@ComponentScan扫描配置没有问题,出错原因是:@ComponentScan扫描只会注入有@Component等注解的类,而不是接口,所以实际上mongo repository的接口并没有实例化注入spring容器。@EnableMongoRepository注解可以指定basePackages,所以配置一下@EnableMongoRepository就可以了。
    解决后的代码如下:

    
    @ComponentScan(basePackages = {"com.jiepuxun.bolipicommon.mongo","com.jiepuxun.bolipiuser"})
    @MapperScan("com.jiepuxun.bolipidao.mapper")
    @EnableCaching
    @EnableMongoRepositories(basePackages = {"com.jiepuxun.bolipicommon.mongo.repository"})
    @SpringBootApplication
    public class BolipiUserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(BolipiUserApplication.class, args);
        }
    
    }
    

    这个结果虽然可以实现,不过就像评论区的老哥说的,启动类越简单越好,所以后续会把注解配置往配置类转移。谢谢大家积极解答问题!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 4月23日
  • 已采纳回答 4月15日
  • 修改了问题 4月14日
  • 修改了问题 4月14日
  • 展开全部

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀