Search code examples
javaspring-bootmavendependency-injectionmicroservices

How to access a controller or a component from one directory to another in Spring Boot?


Tried to access a controller from a parent directory. I have used this method to call it from a child directory.

<dependency>
    <groupId>com.sample</groupId>
    <artifactId>sample-core</artifactId>
</dependency

Here is the error for the above usage

[ERROR] /D:/Projects/sample/src/main/java/com/sample/controllers/abcController.java

package com.sample.controller does not exist

Any help is appreciated to help me get access from a parent or a different directory.


Solution

  • you need to show to spring that contains other classes to scan beyond your current project

    I think you are looking for @ComponentScan(basePackages={"com.sample"}) that tells to spring see your third-party packages

    @SpringBootApplication
    @EntityScan(basePackages = {"br.com.scan-others-entities-in-other-projects"})
    @ComponentScan(basePackages = {"br.com.my-curent-project", "br.com.scan-other-configs-and-controllers"})
    @EnableJpaRepositories(basePackages = {"br.com.com-other-repositories"})
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }