调度程序 servlet 上下文在 Spring 上下文刷新后创建 bean,而不是使用"parent"



当应用程序启动时,将引发两个上下文:一个用于org.springframework.web.context.WebApplicationContext事件,另一个用于org.springframework.web.context.WebApplicationContext:/dispatcher事件,并且一切正常,dispatcher上下文使用"父";而不是自己创造。当我使用ConfigurableApplicationContext::refresh()启动spring上下文刷新时(我使用rest api请求调用它(,只发送一个ContextRefreshEvent(第一个(,并且当我尝试访问任何servlet组件(rest控制器(时,dispatcher开始创建他自己的bean,尽管存在"beans";主";刷新后创建的bean。并且再次没有事件被发送给调度程序上下文刷新。我需要创建dispatcher上下文,以便使用刷新的web应用程序上下文中的bean,而不是创建自己的bean。我认为问题是因为调度程序上下文没有用主上下文刷新。也许它甚至不知道刷新的上下文已经被提出。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-*.xml</param-value>
</context-param>
<listener>
<listener-class>com.app.web.CustomContextListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/config.json</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.map</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.eot</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.woff2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/assets/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.svg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ttf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpeg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.bmp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.app"/>
<import resource="classpath*:configuration-context.xml"/>
<mvc:annotation-driven/>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean class="com.ais.configuration.collection.CustomConfigLoader">
<constructor-arg name="pathsHolder" ref="applicationPaths"/>
<property name="location" value="app.properties"/>
<property name="searchSystemEnvironment" value="true"/>
<property name="ignoreResourceNotFound" value="false"/>
<property name="ignoreUnresolvablePlaceholders" value="false"/>
</bean>
</beans>

dispatcher servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

对于其他想知道这个问题答案的人来说,这就是我解决问题的方法

我创建了实用程序类来存储应用中的所有ApplicationContext

public class ApplicationContextUtils
{
public static final Set<ApplicationContext> AppContexts = new LinkedHashSet<>();
}

然后我编写了一个类来处理所有ContextRefreshed事件,并将相应的上下文放在实用程序类中的集合中

@Component
public class ContextRefreshedEventsHandler implements ApplicationListener<ContextRefreshedEvent>
{
@Override
public void onApplicationEvent( ContextRefreshedEvent event )
{
ApplicationContext app = event.getApplicationContext();
ApplicationContextUtils.AppContexts.add( app );
}
}

因此,当应用程序启动时,所有上下文都按照出现的顺序存储在我的实用程序类中。在我的RefreshService中,我遍历所有上下文并刷新它们

List<ApplicationContext> appContexts = new ArrayList<>( ApplicationContextUtils.AppContexts );
for( ApplicationContext applicationContext : appContexts )
( (ConfigurableApplicationContext)applicationContext ).refresh();

这解决了的问题

最新更新