应用程序上下文被加载两次-Spring引导
我有一个相当简单的设置。一个包含3个模块的maven项目:core/webapp/model。我正在使用Spring boot来提升我的应用程序。在webapp中,我有一个简单的类WebappConfig,如下所示:应用程序上下文被加载两次-Spring引导,spring,tomcat,spring-boot,Spring,Tomcat,Spring Boot,我有一个相当简单的设置。一个包含3个模块的maven项目:core/webapp/model。我正在使用Spring boot来提升我的应用程序。在webapp中,我有一个简单的类WebappConfig,如下所示: @Configuration @EnableAutoConfiguration @ComponentScan(excludeFilters = @ComponentScan.Filter(Configuration.class)) public class WebappConfig
@Configuration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @ComponentScan.Filter(Configuration.class))
public class WebappConfig {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(WebappConfig.class);
app.setAdditionalProfiles("dev");
app.run(args);
}
}
核心/模型模块中的类很少。我的容器应用点是:
public class AbcdXml extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(WebappConfig.class);
}
}
而且没有web.xml!我的型号的pom具有以下与spring boot相关的依赖性:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
没有我前面提到的web.xml
有几件有趣的事情我不明白为什么:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">
</web-app>
API中说“启用Spring应用程序上下文的自动配置,尝试猜测和配置您可能需要的bean。”
还将实例化bean。它将扫描包,查找并注册bean
由于您正在使用这两个注释,我想这就是为什么它会加载两次 如果您的应用程序包含jersey-spring3,而您没有采取步骤禁用,它将尝试为您创建一个
ApplicationContext
(有用,没有)。有一种方法可以将其关闭(在WebApplicationInitializer
中):
servletContext.setInitParameter(“contextConfigLocation”,即“”);
或者使用这个:(包含为依赖项)。在我的案例中,罪魁祸首使用的是Spring Boot 1.3.0.M4以及Jersey 2.21。当我将Spring Boot降级到1.2.6.RELEASE时,问题就消失了。 我唯一需要做的就是显式重写以下属性,因为我需要spring 4.2.0支持Hibernate 5,jackson 2.6.2支持JSR310(java8 java.time):
4.2.0.0版本
2.6.2
编辑:从spring boot 1.3.0.0版本开始,此错误仍然存在。请参见在我的例子中-我遇到了同样的问题-两次看到Spring
启动屏幕
-这是因为我有两个类扩展了SpringBootServletilizer
。一个名为SpringBootWebApplication
,另一个名为servletializer
。我刚刚删除了servletializer
,它工作正常。我不知道为什么会有两个类——可能是因为我从两个不同的例子中得到了灵感来组装我需要的东西。我的主要问题是我的spring上下文被加载了两次。当我打印每个类的类装入器时,我发现我的应用程序运行了两次。(即,当我在按下F9键后在intellij中调试时,我再次在同一行上着陆,即
ConfigurableApplicationContext applicationContext = SpringApplication.run(ConfigAssignServer.class, args);
我的问题出在pom.xml中,我从pom中删除了下面的依赖项,它成功了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
org.springframework.boot
弹簧靴开发工具
运行时
请检查您的依赖关系。希望它能帮助其他人。享受编码:)我也有同样的问题
我有src>main>webapp>WEB-INF>WEB.xml要表达的实体
org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener
神秘的web.xml包含什么?在根文件夹中生成的那一个?在问题中更新。这看起来无害。但是堆栈跟踪中有一个ContextLoaderListener
,这似乎是错误的。也许你可以从一个war文件开始(去掉另外两个)重现问题,然后发布一个指向整个项目的链接?好吧,模型打包是jar,核心是jar,模块之间的依赖关系是常见的。核心需要模块,webapp需要核心。也许你说“甩掉另外两个”的时候我遗漏了什么?反正只有一场战争!只是想减少表面积。也许你的一个罐子里有一个webapplicationinitializer
?我不这么认为!它们都处于不同的环境中。好吧,反正也没用。这也正是我的问题。servletilizer.java
是由Spring启动启动程序自动生成的(例如在STS中创建新项目时)。这也吸引了我,这应该是一个有用的功能:/
servletContext.setInitParameter("contextConfigLocation", "<NONE>");
<spring.version>4.2.0.RELEASE</spring.version>
<jackson.version>2.6.2</jackson.version>
ConfigurableApplicationContext applicationContext = SpringApplication.run(ConfigAssignServer.class, args);
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>