9

I have a Spring boot application and I want to import a dependency written in spring boot that defines some controllers.

Maybe it is straightforward, but how can I made the main application able to initialize all these controllers in the imported module? When I try to access the path to these controllers I get a error for missing handler method for the given path. I tried as follows:

@SpringBootApplication
@ComponentScan(basePackages = {"com.main.project", "com.imported.dependency"})
public class MyApplication
    implements CommandLineRunner {

    public static void main(final String... args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.setWebEnvironment(true);
        app.run(args);

    }
}

i.e. I tried with @ComponentScan, but nothing happens.

I also tried to see if the controllers are loaded:

ApplicationContext ctx = SpringApplication.run(FrontendApplication.class, args);

System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
    System.out.println(beanName);
}

They are not. I tried to remove @SpringBootApplication and to use @EnableAutoConfiguration and @ComponentScan, but this does not work.

Suggestions?

mat_boy
  • 12,998
  • 22
  • 72
  • 116
  • you misspelled "dependency" on ComponentScan. – Meriton Husaj Oct 21 '15 at 14:14
  • Ok, this is a typo in my example. It is not the real package name. The one that I'm using works fine – mat_boy Oct 21 '15 at 14:27
  • Have you tried listing them out in the SpringApplication constructor? e.g. SpringApplication app = new SpringApplication(MyApplication.class, MyAnnotatedBean.class); – Miner_Glitch Oct 23 '15 at 10:44
  • No I didn't! But should I list all the controllers? Doesn't seems to be a practical way! – mat_boy Oct 23 '15 at 10:46
  • How have you imported the dependency? Do you use Maven or Gradle? – adarshr Oct 23 '15 at 10:55
  • Don't do that. Use just the main class in SpringAppplication. The rest should work fine with `@ComponentScan`. Stupid question, but your Controller does have an `@Controller` annotation? It would be easier to help if we saw some code... Also, have you tried moving the controller from your dependency into your main project to see if that makes a difference? – Daniele Torino Oct 23 '15 at 10:58
  • @DanieleTorino yes, the controller is annotated. Yes, it works in the main project. I'll try to post some code, but it will be hard to extract a "not working" example that doesn't includes two poms, one controller and the main application – mat_boy Oct 23 '15 at 11:07
  • Are you sure the modules are imported correctly ? Have you tried already to setup one of the controller you want to use manually with the @Bean annotation? – Emanuele Ivaldi Oct 23 '15 at 11:09
  • @EmanueleIvaldi I'll try! – mat_boy Oct 23 '15 at 11:11
  • Yep it is worth a try, if you have some problems doing that at least you know why the autoscan is not working, if it actually works.. that's weird :) – Emanuele Ivaldi Oct 23 '15 at 11:13
  • @EmanueleIvaldi The `@Bean` annotation works. But I have to create a bean for each controller. Do you have a solution to make this better? Maybe I can define something in the imported module (that I developed) to instantiate the beans with such an annotation directly when I construct the bean. – mat_boy Oct 24 '15 at 11:32
  • @mat_boy Uh that's weird, in the configuration of the module you are importing are you using @ ComponentScan already ? If the answer is yes (you said that the imported module alone is working, so I imagine it is) try to annotate your MyApplication class with this annotation: @ Import(ourImportedModuleConfiguration.class) – Emanuele Ivaldi Oct 24 '15 at 11:46
  • @EmanueleIvaldi The imported module is not a standalone application. All the services and controllers are tested, but I cannot run a spring boot application there. The idea was to define some services and controllers there as an independent module and to use it in my spring boot app. Should I define a configuration for that module? Can you make an example? – mat_boy Oct 24 '15 at 12:07
  • @mat_boy, you can do that (it's a common thing to do) but it's not going to change anything in the end, it's weird already the autoscan is not picking up your controllers (and if you can load them with @ Bean it means their configuration is fine), I've a project that's pretty much configured the same way (the parent war include an "adapter" jar with all the spring mvc controllers in it) and it works alright. It's difficult to say without seeing your code, it may be a pesky error, try to doublecheck the package name in the autoscan configuration. – Emanuele Ivaldi Oct 24 '15 at 12:53
  • @EmanueleIvaldi how. Can you post a possible answer? – mat_boy Oct 24 '15 at 16:20
  • @mat_boy hi, I've replied with a small example I put on github, that works and yours should work a well, give it a try, hope it helps – Emanuele Ivaldi Oct 24 '15 at 16:25
  • 1
    @EmanueleIvaldi I see! Grazie! – mat_boy Oct 24 '15 at 16:47
  • No problem, let me know how it goes! – Emanuele Ivaldi Oct 24 '15 at 17:09

4 Answers4

3

after the discussion on the main thread I tried to setup a small project that's like yours and I put it on github, I can't see any problem.

Give it a look https://github.com/e-ivaldi/mat_boy_test

This is from the log 2015-10-24 17:22:02.900 INFO 31901 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/**]}" onto public java.lang.String com.somethingelse.controllers.SimpleController.xxx()

Emanuele Ivaldi
  • 1,332
  • 10
  • 15
1

The @SpringBootApplication will automatically scan every class that has a sub-package namespace from every jar in the classpath. That annotation is all you need, given your project follows Spring's recommended directory structure. See: Spring Boot's Documentation on Structuring your code.

Try the following:

  1. Remove this line of code:

    @ComponentScan(basePackages = {"com.main.project", "com.imported.dependency"})
    
  2. MoveMyApplication so that it is located in your root directory. The root directory should follow this naming convention, com.example.project. So the fully qualified path for your spring boot application main class would be: com.example.project.MyApplication, while replacing example and project with your company host name and project name.

  3. Put your controllers in a sub-package of that (even when packaged in a separate jar). So their namespace should be something like this: com.example.project.controllers.

  4. Also, don't forget to add the @Controller or @RestController annotation to your controller classes.

Hope this helps!

cosbor11
  • 14,709
  • 10
  • 54
  • 69
0

Maybe you have a conflict between:

@SpringBootApplication and @ComponentScan.

In Spring Boot documentation we can read

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes

The link: @SpringBootApplication documentation

Can you remove your @SpringBootApplication and replace it by @Configuration and @EnableAutoConfiguration ?

fabballe
  • 761
  • 4
  • 12
0

use @Configuration and @EnableAutoConfiguration annotation.

s_u_f
  • 200
  • 1
  • 12