1
I’m using Spring Boot to create a Restful web service and a SOAP, each in a module but both in a single project. So I decided to separate my domain layer into a third module since it’s the same for both types of web services and so could reuse. Even with the dependency between packages in Maven, when I run the application, it questions that, for example, PessoaRepository
, which is the domain module and is being injected via @Autowired
in Resource PessoaResource
restful module, could not be injected by not being a Bean
recognized by Spring:
Field pessoaRepository in br.com.tassioauad.restful.resource.PessoaResource required a bean of type 'br.com.tassioauad.domain.repository.PessoaRepository' that could not be found.
My breakup was like this:
Domain Module
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.tassioauad</groupId>
<artifactId>domain</artifactId>
<version>0.5</version>
<packaging>jar</packaging>
<name>domain</name>
<description>Camada de Domínio</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies>
</project>
Pessoarepository.java
public interface PessoaRepository extends CrudRepository<Pessoa, Integer>{
//....
}
Módulo Restful
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.tassioauad</groupId>
<artifactId>restful</artifactId>
<version>0.5</version>
<packaging>jar</packaging>
<name>restful</name>
<description>RESTFul API</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>br.com.tassioauad</groupId>
<artifactId>domain</artifactId>
<version>0.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Pessoaresource.java
import br.com.tassioauad.domain.repository.PessoaRepository;
@RestController
@RequestMapping("/pessoa")
public class PessoaResource {
@Autowired
PessoaRepository pessoaRepository;
//...
}
What is missing so that the Repository
of one module is recognized in another module?
I had a problem similar to this, with similar solution. I put
@ComponentScan
in my solution, he was not finding a dependency of mine annotated with@Component
– Jefferson Quesado
I believe that Spring will not scan components in dependencies, as it would be very expensive. Imagine if he did it in every dependency to find out if there are any components... Just now I thought about it.
– Tássio Auad
Within the project itself, an automatic scan is very feasible and necessary, because there will surely be components there. I’ve never dealt with that in separate modules.
– Tássio Auad
I usually sort out a single package with the things that Spring needs to look at on the dependencies and point out the
Scan
there too. It saves his time. It has served me well so. There have been some projects and 2 dependencies so successfully– Jefferson Quesado
Excellent comment, @Jeffersonquesado. It is recorded there this tip. I will make some tests in this model!
– Tássio Auad