Spring boot modules via maven do not interact

1

I'm using Spring Boot to create a web service RESTFul 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 is the same for both types of web services and so I could reuse. Even with the dependency between the packages in Maven, when I run the application, it asks that, for example, PessoaRepository , which is in the domain module and is being injected via @Autowired into the resource PessoaResource of the restful module, not could be injected for not being% acknowledged 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 separation looks 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>

PersonRepository.java

public interface PessoaRepository extends CrudRepository<Pessoa, Integer>{

    //....

}

RESTFul 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>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>

PersonaResource.java

import br.com.tassioauad.domain.repository.PessoaRepository;

@RestController
@RequestMapping("/pessoa")
public class PessoaResource {

    @Autowired
    PessoaRepository pessoaRepository;

    //...
}

What is missing so that Bean of one module is recognized in another module?

    
asked by anonymous 23.03.2018 / 14:55

1 answer

1

This usually happens when you split the project into multiple modules. It turns out that Spring is unable to perform scan from components, entities, repositories, and services from one module to another in certain situations. Therefore, it is important to tell the basic path from where you are in order for it to scan . In case, we are dealing with JPA repositories, so let's put annotation @EnableJpaRepositories in the class that contains the main method:

@EnableJpaRepositories("br.com.tassioauad.domain")
@SpringBootApplication
public class RestfulApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestfulApplication.class, args);
    }
}

This still will not solve everything, there will be an error stating that you have not found the Person entity. That's because it only became able to scan in JPA repositories, but not in @Entity . For this we will add another anottation and now everything will be OK:

@EnableJpaRepositories("br.com.tassioauad.domain")
@EntityScan("br.com.tassioauad.domain")
@SpringBootApplication
public class RestfulApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestfulApplication.class, args);
    }
}
    
23.03.2018 / 14:55