Spring Boot Jersey - 运行java -jar命令时缺少EmbeddedServletContainerFactory

时间:2014-11-03 13:11:58

标签: java maven-3 spring-boot executable-jar jersey-2.0

使用java -jar命令运行Spring Boot Jersey应用程序时遇到问题。当我从IntelliJ运行主类时它工作得很好。我因此认为这可能是一个类路径问题,但我使用了maven-assembly-plugin,并且所有的Spring类似乎都在那里。

我有:  类路径上的spring-boot-starter-web,spring-boot-jersey,spring-boot-starter-tomcat以及Spring引导的其余部分我已经包含了这个:

    <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>

我的程序集插件配置如下:

            <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>

当我解压缩jar文件时,我发现所有Spring依赖项都存在(EmbeddedServletContainer及它的子类(Tomcat / Jetty))。

启动日志显示以下错误消息:

:: 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)

我试图明确定义一个EmbeddedServletContainerFactory,但这只是给了我另一个例外,其中有一条消息说A ServletContext is required to configure default servlet handling

对此问题的任何帮助将不胜感激!

编辑!

我尝试通过实现BeanPostProcessor来打印容器创建的每个bean:

@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;
}

}

在IntelliJ中运行它会给我以下输出:

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

使用java命令运行jar文件会给出以下输出:

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)

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在我更彻底地阅读文档之后,我终于明白了。 Spring docs明确指出您需要在pom.xml的build部分中使用spring-boot-maven-plugin。

<build>
    <plugins>
       <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
       </plugin>
    </plugins>
</build>  

我还添加了一个声明main-class的属性:

<properties>
   <start-class>org.online.auth.ApplicationMain</start-class>
</properties>

希望这对其他人有帮助。