Reputation: 1121
I have a multiple modules Spring-boot application, I have two submodules:
that are organized like this:
persistence module
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tutorials</artifactId>
<groupId>com.medkhelifi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>persistence</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
spring-boot-rest-api-todo-list module
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tutorials</artifactId>
<groupId>com.medkhelifi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-rest-api-todo-list</artifactId>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>com.medkhelifi.tutorials.springboot.restapi.todolist.RestTodoListApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.medkhelifi</groupId>
<artifactId>persistence</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-rest-api-todo-list</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
In my rest-api module, I want to scan both modules (persistence and restapi) and I proceed like this:
@SpringBootApplication
@ComponentScan ({"com.medkhelifi.tutorials.springboot", "com.medkhelifi.tutorials.persistence"})
public class RestTodoListApplication {
@Autowired
private UserRepository userRepository;
public static void main (String[] args){
SpringApplication.run(RestTodoListApplication.class, args);
}
}
But I got this error :
Field userRepository in com.medkhelifi.tutorials.springboot.restapi.todolist.RestTodoListApplication required a bean of type 'com.medkhelifi.tutorials.persistence.model.repositories.UserRepository' that could not be found.
Action:
Consider defining a bean of type 'com.medkhelifi.tutorials.persistence.model.repositories.UserRepository' in your configuration.
If I remove "com.medkhelifi.tutorials.spring-boot"
from ComponentScan the error disappear but my controllers under my restapi module will not work.
I tried many variations of ComponentScan, but with the same error:
@ComponentScan ({"com.medkhelifi.tutorials"})
@ComponentScan ({"com.medkhelifi.tutorials.persistence", "com.medkhelifi.tutorials.springboot"})
Upvotes: 1
Views: 3237
Reputation: 1121
I think I found the solution, I added @EnableMongoRepositories
to scan my persistence module:
@EnableMongoRepositories(basePackages = "com.medkhelifi.tutorials.persistence")
Upvotes: 2
Reputation: 950
Your Auto wiring Happening in Main Application class which you have not provided as base package for component scanning. Try this
@ComponentScans(value = { @ComponentScan("com.medkhelifi.tutorials"),
@ComponentScan("com.medkhelifi.tutorials.persistence"), @ComponentScan("com.medkhelifi.tutorials.restapi") })
OR
@ComponentScan(value = {"com.medkhelifi.tutorials",
"com.medkhelifi.tutorials.persistence", "com.medkhelifi.tutorials.restapi" })
Upvotes: 1
Reputation: 599
Spring is trying to look for the Autowired dependency but it's not finding it anywhere.
Try adding this:
@EnableJpaRepositories({"com.medkhelifi.WhereverYourRepoIs"})
If I'm not mistaken you need to tell SpringBoot where to find your repos, specially if they are in a different module than your @SpringBootApplication.
Upvotes: 0