gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:41 25 4
gpt4 key购买 nike

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

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

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

如有任何帮助,我们将不胜感激!

最佳答案

仔细阅读文档后,我终于弄明白了。 Spring docs明确指出您需要在 pom.xml 的构建部分中使用 spring-boot-maven-plugin。

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

我还添加了一个声明主类的属性:

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

希望这对其他人有帮助。

关于java - Spring Boot Jersey - 运行 java -jar 命令时缺少 EmbeddedServletContainerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26715130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com