Slava
Slava

Reputation: 155

@ComponentScan from 2 packages

I have 2 projects, the main spring boot project and the second spring project, which contains @Contollers, @Services and so on.

I try to use some of this services in main project. I saw similar questions like here, here, here and many others, but it does not work for me

1) if I have just

@SpringBootApplication
public class App { ... }

then it does not compile because can not find services from the second project

2) if I try to scan the second package

@SpringBootApplication
@ComponentScan("secondPackage")
public class App {}

then the application starts without errors, but controller mapping does not work at all. Mapping from both projects can not be resolved.

3) then I tried exclude controllers from second package, because I do not need them, mapping still does not work

@ComponentScan(basePackages="secondPackage", excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)})

4) then I tried to scan for particular services, but in this case other resources are not available and I have to include to much paths

@ComponentScan({"first package", "second package"})

5) finally I tried to make 2 configuration files

@Configuration
@ComponentScan(basePackages = {"mainPackage"})
public class MainConfiguration {

}

@Configuration
@ComponentScan(basePackages="secondPackage", 
        excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)})
public class SecondConfiguration {

}

but I did not found example, how I can prepare context before SpringApplication.run(App.class, args); and then make the app run with my context. like

ApplicationContext myContext = new AnnotationConfigApplicationContext(OrderStatusConfiguration.class, ShopRuServicesConfiguration.class);
SpringApplication.run(App.class, myContext, args);

So please give me an advice, why mapping does not work or how should I proper autowire services from the second project!

I appreciate any help! Thanks!

Upvotes: 4

Views: 10207

Answers (1)

1) An empty annotation triggers a default component scan. This is starting at the package of the annotated class

@SpringBootApplication
public class App { ... }

Than you say it does not even compile.

This means, your spring confgiguration references some classses by name which are not in the classpath.

2) If you add @ComponentScan the default component scan of 1) is not done, instead scan is done only starting at 'secondPackage'

@SpringBootApplication
@ComponentScan("secondPackage")
public class App {}

Than you write

mapping does not work at all

This could mean, that the controller are not included in the spring configuration at all.

To resolve this you may try

@SpringBootApplication
@ComponentScan({"package of App","secondPackage"})
public class App {}

which may give you the result you expected from 2)

If this doe not work, I would suggest to create java configuration classes in the referenced projects, that only configure, what you need in your app.

Use @Import to import those configurations. This way you reference the configuration classes in your app, and verify your classpath is correctly configured.

If your app still does not configure the services and mappings correctly, you can fine tune the referenced configurations until you get exactly the spring context you need.

Upvotes: 1

Related Questions