简体   繁体   English

SpringBoot scanBasePackages在不同的jar中找不到存储库

[英]SpringBoot scanBasePackages does not find repository in different jar

I have a spring boot application like this: 我有一个像这样的spring boot应用程序:

package my.package;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;

@SpringBootApplication
public class MySpringBootApp{

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApp.class, args);
    }

}

I have a service in the package my.package.service 我在包my.package.service有一个服务

@Service
public class MyService {

private ServiceInADifferentJar  dep;

public MySerivce(ServiceInADifferentJar dep) {
this.dep = dep;
}

}

The class ServiceInADifferentJar is an @Service annotated class in a different JAR, which I include as a maven dependency. ServiceInADifferentJar类是不同JAR中的@Service注释类,我将其作为maven依赖项包含在内。

The JAR Has this file structure: JAR具有以下文件结构:

src/main/java
- some.package.repository
    MyRepository.java
- some.package.service
    ServiceInADifferentJar.java

MyRepository is an @Repository annotated interface that extends a Spring Data inerface. MyRepository是一个@Repository注释接口,它扩展了Spring Data接口。

ServiceInADifferentJar gets MyRepository injected in its constructor. ServiceInADifferentJar在其构造函数MyRepository注入了MyRepository

WHen I start the application, I get an error that ServiceInADifferentJar cannot be found. 当我启动应用程序时,我收到一个无法找到ServiceInADifferentJar的错误。

Then I added this to my SpringBootApp 然后我将它添加到我的SpringBootApp中

@SpringBootApplication(scanBasePackages = {"some.package"})

and ServiceInADifferentJar is found, but not MyRepository . 找到了ServiceInADifferentJar ,但没有找到MyRepository

Why not? 为什么不? Why aren't all sub-packages of some.package in the other JAR scanned? 为什么不扫描其他JAR中some.package所有子包?

* EDIT * *编辑*

The repository 存储库

package some.package.repository;

    @Repository
    public interface MyRepository extends MongoRepository<SomeEntity, String> {

    }

You may want to use the EnableMongoRepositories annotation, so that your Mongo repository gets found . 您可能希望使用EnableMongoRepositories批注,以便找到您的Mongo存储库。

@EnableMongoRepositories(basePackages = "some.package.repository")

The following question, despite being about JPA repositories, has some more explanation about how repositories scanning work : 以下问题,尽管是关于JPA存储库,但有关于存储库扫描如何工作的更多解释:

Can't Autowire @Repository annotated interface in Spring Boot 无法在Spring Boot中自动加载@Repository注释接口

嘿,你应该在@SpringBootApplication标签@ComponentScan(basePackages = {“some.package”})后把它放在你的主类中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM