As my first step in mastering Java 9 modules, I'm first creating a Java program that resides in nested directories, all joined by a root-level Maven pom.xml. That is, Java modules aren't yet involved.
The source for my program is a Spring Boot program that already works. I have divided that program into a controller (access Maven module) and data access (persistence Maven module). My tutor for this task has been this Spring site: https://spring.io/guides/gs/multi-module/ .
I'm running this thru Visual Source Code, with Java 11 in my JAVA_HOME. The Spring Boot program starts, then yields this sort of error:
Parameter 0 of constructor in com.logicaltiger.persistence.service.LocationService required a bean of type 'com.logicaltiger.persistence.repository.LocationRepository' that could not be found.
The program worked before I sliced / diced it, so my problem is with Spring configuration.
My request? Can my configuration be fixed so that Spring Boot finds all of its parts?
The AccessApplication has this:
@SpringBootApplication(scanBasePackages = "com.logicaltiger")
@ComponentScan("com.logicaltiger")
@EnableAutoConfiguration
public class AccessApplication {
public static void main(String[] args) {
SpringApplication.run(AccessApplication.class, args);
}
}
LocationService has this:
@Service
[snip]
private final LocationRepository locationRepository;
public LocationService(LocationRepository locationRepository) {
this.locationRepository = locationRepository;
}
LocationRepository has this:
@Repository
public interface LocationRepository extends CrudRepository<Location, Long> {
This is my directory structure, ignoring.mvn, etc.
district-updater-modules
access
src
main
java
com
logicaltiger
access
controller
(various controller files, with @Controller annotation)
util
Utilities.java
AccessApplication.java
resources
static
templates
application.properties
pom.xml
persistence
main
java
com
logicaltiger
persistence
model
(various model files)
repository
LocationRepository.java
service
LocationService.java
util
Utilities.java
AccessApplication.java
resources
pom.xml
pom.xml
The root pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" [snip] >
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicaltiger</groupId>
<artifactId>district-updater-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>district-updater-modules</name>
<description>Wrapper for the modules of DistrictUpdater -- Java modules</description>
<packaging>pom</packaging>
<modules>
<module>access</module>
<module>persistence</module>
</modules>
</project>
The access pom.xml (some dependencies omitted):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" [snip] >
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.logicaltiger.access</groupId>
<artifactId>access</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>district-updater-modules-access</name>
<description>DistrictUpdater web API access module</description>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.logicaltiger</groupId>
<artifactId>persistence</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The persistence pom.xml (some dependencies omitted):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" [snip] >
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.logicaltiger</groupId>
<artifactId>persistence</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>district-updater-modules-persistence</name>
<description>DistrictUpdater persistence module</description>
<packaging>jar</packaging>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Thanks,
Jerome.
After a lot of surfing and experimenting, I finally found the solution here .
In addition to @ComponentScan("com.logicaltiger")
you also need
@EnableJpaRepositories(basePackages = "com.logicaltiger")
in order to find all repositories, @EntityScan("com.logicaltiger.*")
in order to find all entities.Hence:
@SpringBootApplication(scanBasePackages = "com.logicaltiger")
@ComponentScan("com.logicaltiger")
@EnableJpaRepositories(basePackages = "com.logicaltiger")
@EntityScan("com.logicaltiger.*")
@EnableAutoConfiguration
public class AccessApplication {
public static void main(String[] args) {
SpringApplication.run(AccessApplication.class, args);
}
}
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.