1

I am a new bee and using microservices(Spring Boot, Spring Cloud) in which I am trying to use resource file of a microservice in another. For that I need to scan that module in another one via ComponentScan.

Like I have an Admin module in which I need to autowired Main Resource that is in main module.So I use:

@ComponentScan(basePackages = {"com.example.admin","com.example.main"}

I used this in AdminApplication file.Now it also shows Main module's Controllers in Admin which I don't want. I google it and apply:

  @ComponentScan(basePackages =
      {"com.example.admin","com.example.main"},
              excludeFilters = {@ComponentScan.Filter(type = ASSIGNABLE_TYPE,
                      value = {
                              UserController.class,
                              CustomerController.class,
                              SchoolController.class
                      })})

But it still shows this Main module controllers in Admin Module. How to actually exclude this? Please help me.

maddy
  • 248
  • 1
  • 5
  • 16
  • why do you want them to get excluded anyway? – Syed Anas Sep 07 '18 at 10:43
  • I am using swagger for testing purpose. In which I need only its own controller to test (admin) and don't want to see main controllers in admin path. – maddy Sep 07 '18 at 10:57
  • Can you also share the folder structure of your micro services? – Syed Anas Sep 07 '18 at 11:15
  • Each Microservice have its MVC. and microservices communicate via Eureka and zuul – maddy Sep 07 '18 at 11:19
  • This problem arise when I try to autowired a resouce class of main in controller of admin. That time I need to scan main module too to get repository,resource and other classes. – maddy Sep 07 '18 at 11:23
  • if you need them for autowiring then you need to add them to your component scan path otherwise it will throw an exception of unsatisfied dependency. For swagger even if it shows on the ui unless you invoke it its not gonna hurt so i would suggest that you instead of trying to remove from contxt. keep them in spring context and find a workaround from your testing – Syed Anas Sep 07 '18 at 12:12
  • @Anas ok Thank you for your advice. Let me think about it. – maddy Sep 07 '18 at 12:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179634/discussion-between-anas-and-maddy). – Syed Anas Sep 07 '18 at 12:20

2 Answers2

0

With JavaConfig (@Configuration) and the @Profile annotation, you could set up a subtile combination of classes "in and out" depending of your needs, I guess. BUT you would have to disable @ComponentScan on your main class (don't use @SpringBootApplication, maybe, as it's embedding @ComponentScan).

IMHO you should rather modularize your applications/services, building common resources as a separated JAR, and each service as a distinct Maven module depending on it

Thomas Escolan
  • 1,298
  • 1
  • 10
  • 28
0

Thanks for your suggestions. Finally I got the answer.

Swashbuckle is built on top of WebApi's built-in metadata layer - ApiExplorer. If you decorate a controller or action with the following attribute:

[ApiExplorerSettings(IgnoreApi=true)]
public class MyController : ApiController

then this will ultimately cause the entire controller or individual action to be omitted from the Swagger output .

maddy
  • 248
  • 1
  • 5
  • 16