0

We're starting a Spring Boot app with gradle bootRun which triggers the x.Application class which has the annotation @SpringBootApplication. After starting we can access REST services in the x package but can't access REST services in other packages. How can you configure the classpath accordingly?

1
  • 1
    Sound like a component scan problem. It's recommended to put the "Application" class in a "root" package, that way the scanning will traverse child packages recursively. Otherwise, you can add a @ComponentScan to scan other packages Commented Feb 29, 2016 at 7:24

1 Answer 1

1

Assuming your x package is something like com.example.mybootapp and your main Application.class is inside x package then you need to add this

@SpringBootApplication
@ComponentScan({"com.example.mybootapp","com.example.someother","one.more.pack"})

on your main Application.class method or Configuration file.

@SpringBootApplication itself consists of @Configuration @EnableAutoConfiguration @ComponentScan annotations, so @ComponentScan default the basePackge (i.e. packages to scan) to the package of the main Application.class and that is why Spring is unable to detect your other @Controlers which are outside of main package.

If you structure your code as suggested above (locating your application class in a root package), you can add @ComponentScan without any arguments. All of your application components (@Component, @Service, @Repository, @Controller etc.) will be automatically registered as Spring Beans.

Refer this document on how to structure your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. @ComponentScan({"com.example.mybootapp","com.example.someother","one.more.pack"}) did the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.