简体   繁体   中英

SpringBoot scanBasePackages does not find repository in different jar

I have a spring boot application like this:

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

@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.

The JAR Has this file structure:

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.

ServiceInADifferentJar gets MyRepository injected in its constructor.

WHen I start the application, I get an error that ServiceInADifferentJar cannot be found.

Then I added this to my SpringBootApp

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

and ServiceInADifferentJar is found, but not MyRepository .

Why not? Why aren't all sub-packages of some.package in the other JAR scanned?

* 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(basePackages = "some.package.repository")

The following question, despite being about JPA repositories, has some more explanation about how repositories scanning work :

Can't Autowire @Repository annotated interface in Spring Boot

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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