spring boot modules via Maven do not interact

Asked

Viewed 592 times

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?

1 answer

1


This usually happens when you separate the project into several modules. It turns out that Spring cannot perform the scan of components, entities, positions and services from one module to another in certain situations. Therefore, it is important to inform the basic path from where they are to carry out the scan. In this case, we are dealing with JPA repositories, so let’s put Annotation @EnableJpaRepositories in the class containing the main method:

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

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

This will not solve everything, there will be an error stating about not having found the entity Pessoa. That’s because he only became able to do scan in JPA repositories, but not in @Entity. To 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);
    }
}
  • 1

    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

  • 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.

  • 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.

  • 1

    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

  • 1

    Excellent comment, @Jeffersonquesado. It is recorded there this tip. I will make some tests in this model!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.