3

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!

Community
  • 1
  • 1
Slava
  • 133
  • 2
  • 8
  • I think that the idea of sharing code between two different SpringBoot applications contexts goes against the whole idea of having two applications. I would suggest to create a third SpringBoot project with the shared code and expose the functionality via RestAPIs. – Fernando Feb 15 '17 at 14:14
  • the second application is regular spring mvc application, and about 30 % of classes are needed in the first spring boot application. implementing rest service was my first idea, but it is not an option, because then the whole work will be performed on the one server, where is located the second application. that server is already overloaded – Slava Feb 15 '17 at 14:20
  • of course I can just copy all code, but than in case of changing, I will have to change several places instead of one. I think, there are must be some way to reuse services from another module, which is included as dependency – Slava Feb 15 '17 at 14:22
  • Well, the shared code should not be put in a Web Application. Couldn't you just extract all those services to a different project (let's say it is a JAR) and add the dependency to that project in the Spring Boot app and in the old app? (Again, this is not a good practice anyway, introducing common dependencies to a JAR is also against modularity) – Fernando Feb 15 '17 at 15:00
  • that project can not be modified by me, I know all that options, but now I have a special question, so please give a suggestion, how to solve it. All other workaround I know by myself, so if you do not know who configure java annotations, then let me stop you. – Slava Feb 15 '17 at 15:05

1 Answers1

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.

  • Thank you @Stefan, I tried @ComponentScan({"first","second"}) and in this case the application is compiled and I can call controllers from main app class, but mapping is still broken. The strange thing here is, that there are about 3 mapped url in 1-s application and about 4 paths in 2-d module and they both are broken. I will understand, if one of them will work. It will mean, that some configuration rewrite another, but they both are broken in this case. Could you give me an example of configuration for import? Suppose I need to autowire one service from second project in base app controller – Slava Feb 18 '17 at 15:20
  • The problem here is, that ComponentScan break mapping and without ComponentScan the application will not see another package and also configuration class. So I should try something like @Import({ AppConfigWeb.class }) @ImportResource("classpath:/config/spring.xml")? – Slava Feb 18 '17 at 15:24