0

I have a spring REST application, which works fine if ran using mvn exec with embedded tomcat (beans get created fine, @Autowired works as expected, etc.).

If, however, I create a WAR using mvn package and deploy it into an existing tomcat, some bean creator functions do not get called.

For example, I have a bean creator function

@Bean @Autowired public ObjectMapper objectMapper(LocalizedMessages messages) { // create and configure ObjectMapper with custom serializer } 

Which gets called when with an embedded tomcat, and does not get called when using war. I tried to add the containing class to the SpringApplicationBuilder's sources, but that did not have any effect.

Does anyone have a suggestion how I could get the war version to work properly?

Some extra details

I am using spring-boot 1.2 with the spring-boot-starter-web maven dependency, and originally I was using an App class with a main function to create and start a new SpringApplication (which in turn started an embedded tomcat), and also the App class has appropriate @ComponentScan and @EnableAutoConfiguration annotations so that all @Components and @Configurations are appropriately handled.

With the war packaging, I created a class that extends SpringBootServletInitializer and has a @SpringBootApplication annotation that adds the above App.class to the SpringApplicationBuilder's source list (see generating a working war file with maven for a spring-boot web application). I even tried to list all @Components among the sources, but the above mentioned bean creator functions are still not called.

3
  • Did you specify <context:component-scan/>, *<mvc:annotation-driven/>` or something alike in the war's application context? Commented Mar 7, 2015 at 9:13
  • @Alexander I do not use any spring xml configuration at all, I am using spring-boot with spring-boot-starter-web. Commented Mar 7, 2015 at 9:39
  • ah ok. please add this information to your post Commented Mar 7, 2015 at 12:57

1 Answer 1

0

If anyone is interested, a collegaue of mine found the solution:

It seems that the App class had to extend SpringBootServerInitializer (not an other class), and define itself among the sources in the SpringApplicationBuilder. After that, @ComponentScan began to work magically.

I hope this helps soneone. :)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.