3

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"})

andServiceInADifferentJar 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> {

    }
user3629892
  • 2,960
  • 9
  • 33
  • 64

2 Answers2

2

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

Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • Thanks, that was it! I am a little surprised that Spring actually needs these kind of explicit pointers to those packages, but well, at least it works now. Thanks again! – user3629892 Jun 05 '18 at 15:59
0

hey man you should put this in your main class after the @SpringBootApplication tag @ComponentScan(basePackages = {"some.package"})