Spring Boot Jersey - Missing EmbeddedServletContainerFactory when running java -jar command
I am having problems with my Spring Boot Jersey application when started with a command java -jar
. When I run the main class from IntelliJ it works fine. So I assumed it might be a class issue, but I used the maven-assembly-plugin and all Spring classes seem to exist.
I have: Spring-boot-starter-web, Spring-boot-jersey, Spring-boot-starter-tomcat on the classpath, and for the rest of the Spring boot I have included this:
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.1.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
My build plugin config looks like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<!-- nothing here -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.online.auth.conf.ApplicationMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
When I extract the jar file, I found that all Spring dependencies (EmbeddedServletContainer and its subclasses (Tomcat / Jetty)) exist.
The startup log displays the following error message:
:: Spring Boot ::
2014-11-03 12:46:38.394 INFO 1264 --- [ main] org.online.auth.conf.ApplicationMain : Starting ApplicationMain on R9013DA2 with PID 1264 (C:\projects\online\online-authentication-rest\target\online-authentication-rest-0.0.1-SNAPSHOT-jar-with-dependencies.jar started by thomasa in C:\projects\online\online-authentication-rest\target)
2014-11-03 12:46:38.396 DEBUG 1264 --- [ main] o.s.boot.SpringApplication : Loading source class com.online.auth.conf.ApplicationMain
2014-11-03 12:46:38.412 DEBUG 1264 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Skipped config file 'file:./config/application.yml' resource not found
2014-11-03 12:46:38.416 DEBUG 1264 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'classpath:/application.properties'
2014-11-03 13:36:23.613 INFO 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43738a82: startup date [Mon Nov 03 13:36:23 CET 2014]; root of context hierarchy
2014-11-03 13:36:23.614 DEBUG 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Bean factory for org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43738a82: org.springframework.beans.factory.support.DefaultListableBeanFactory@25b485ba:
defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework .context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.conte xt.annotation.internalCommonAnnotationProcessor,applicationMain]; root of factory hierarchy
2014-11-03 13:36:23.787 INFO 9732 --- [ main] a.ConfigurationClassBeanDefinitionReader : Skipping bean definition for [BeanMethod:name=basicHttpCredentialsUtil,declaringClass=no.safetel.online.auth.AppConfigSpringSecur ity]: a definition for bean 'basicHttpCredentialsUtil' already exists.
This top-level bean definition is considered as an override.
2014-11-03 13:36:23.873 INFO 9732 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2014-11-03 13:36:23.888 DEBUG 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@2dde1bff]
2014-11-03 13:36:23.888 DEBUG 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Using ApplicationEventMulticaster [org.springframework.context.event.SimpleApplicationEventMulticaster@15bbf42f]
2014-11-03 13:36:23.894 WARN 9732 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
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:124)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
I tried to define the EmbeddedServletContainerFactory explicitly, but that just gave me another exception with a message A ServletContext is required to configure default servlet handling
.
Any help on this would be much appreciated!
Edit
I tried to print every bean created by the container by implementing BeanPostProcessor
:
@ComponentScan
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
DataSourceInitializer.class,
DataSourceTransactionManagerAutoConfiguration.class,
SecurityAutoConfiguration.class})
public class ApplicationMain extends SpringBootServletInitializer implements BeanPostProcessor {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(ApplicationMain.class, args);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("\t\tbean: "+ beanName);
return bean;
}
}
Running this in IntelliJ gives me the following output:
2014-11-03 16:11:26.348 INFO 13548 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'metaDataSourceAdvisor' of type [class org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
bean: org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat
bean: tomcatEmbeddedServletContainerFactory
bean: org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration
bean: org.hibernate.validator.internal.constraintvalidators.NotNullValidator
bean: serverProperties
bean: org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration
2014-11-03 16:11:26.889 INFO 13548 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
Running the jar file with the command java
gives me the following output:
2014-11-03 16:01:36.302 INFO 6704 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'metaDataSourceAdvisor' of type [class org.springframework.security.access.intercept.aopalliance.MethodSecurityMetadataSourceAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-11-03 16:01:36.310 WARN 6704 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
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:124)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
Any help would be greatly appreciated!
source to share
I finally figured it out by reading the documentation more carefully. The Spring docs clearly state that you need to use the spring-boot-maven-plugin in the build section of your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I also added a property that declares the main class:
<properties>
<start-class>org.online.auth.ApplicationMain</start-class>
</properties>
Hope this can help others.
source to share