0
@Configuration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication

public class InitService extends SpringBootServletInitializer { 

    public static void main(String[] args) {
    SpringApplication.run("classpath:abc-server.xml", args);
    }
}

++++++++++++++++++++++++++++++++++++++++++

Here i am trying to migrate the Spring MVC project to Spring boot Standalone jar with embedded tomcat. So i tried loading the context xml(abc-server.xml) used in the existing project. When i run/deploy the spring boot jar, the following exception is thrown.

++++++++++++++++++++++++++++++++++++

[2015-05-19 15:12:30,012] ERROR org.springframework.boot.SpringApplication  - Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is     org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.gogo.asp.server.init.InitService.main(InitService.java:188)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
... 13 more
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242

1 Answers1

2

When you call run, you're only providing server-abc.xml as a source for your application's configuration:

SpringApplication.run("classpath:abc-server.xml", args);

That means that InitService is being ignored, including the fact that you've enabled auto-configuration. Without auto-configuration being switched on, Spring Boot will not automatically configure an embedded servlet container for you. You need to provide both InitService and abc-server.xml as configuration for your application.

I would provide InitService.class to SpringApplication.run and use @ImportResource to pull in your old XML configuration:

@SpringBootApplication
@ImportResource("classpath:abc-server.xml")
public class InitService {

    public static void main(String[] args) {
        SpringApplication.run(InitService.class, args);
    }
}

Note that @SpringBootApplication is equivalent to @ComponentScan, @Configuration, and @EnableAutoConfiguration. You can just use @SpringBootApplication and drop the other three annotations as I've done above.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • ThanQ Andy Wilkinson :) – Balasubramanian Ramar May 19 '15 at 13:23
  • Inside the existing codebase, we have used org.springframework.context.support.AbstractRefreshableConfigApplicationContext and org.springframework.context.support.AbstractApplicationContext classes . So if i go with @ImportResource() , i am getting this exception "org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.context.support.AbstractRefreshableConfigApplicationContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency" – Balasubramanian Ramar May 19 '15 at 13:36
  • That's a rather strange dependency to be injecting. Do you really need access to that specific type? Why can't you autowire `ApplicationContext` or implement `ApplicationContextAware`? – Andy Wilkinson May 19 '15 at 14:21
  • i'm migrating the project to Spring Boot, i dono , why they have used **AbstractRefreshableConfigApplicationContext‌** and `AbstractApplicationContext` . So can i replace those classes with `ApplicationContext`? Would there be any issue? – Balasubramanian Ramar May 19 '15 at 15:16
  • I do not know why that exception is coming though i keep the necessary jars properly. If i go with same classes, it would be easy to migrate. – Balasubramanian Ramar May 19 '15 at 15:30
  • i am getting the following exception `AnnotationConfigEmbeddedWebApplicationContext cannot be cast to org.springframework.web.context.support.AbstractRefreshableWebApplicationContext`. this is my code `protected AbstractRefreshableWebApplicationContext rootContext` `public SpringComponentOrchestrator(ApplicationContext rootContext) { this.rootContext = (AbstractRefreshableWebApplicationContext) rootContext; invalidPathList = Arrays.asList(invalidScanPathArray) }` – Balasubramanian Ramar May 20 '15 at 06:49
  • Why are you trying to cast it to `AbstractRefreshableWebApplicationContext`, i.e. what methods are you calling that are specific to that class? Casting to `WebApplicationContext` or even `ApplicationContext` may well be sufficient, but it's impossible to say for sure without seeing your code. You'd probably be better asking a separate question for this part of the problem. – Andy Wilkinson May 20 '15 at 08:48
  • Yes u r right! Mr.Andy Wilkinson. I am going to use `AbstractApplicationContext` instead of those classes. Now it works :) thanks a lot! – Balasubramanian Ramar May 20 '15 at 10:08