[英]Spring ApplicationContext Running twice
I want to migrate Spring WebMvcConfigurerAdapter Configuration with Spring security. 我想通过Spring安全性迁移Spring WebMvcConfigurerAdapter Configuration。 Now i get the problem that i get 2 ApplicationContextes and my Singelten Classes are Statefull.
现在我遇到的问题是我得到了2个ApplicationContextes,并且我的Singelten类是Statefull的。 Here my web.xml and my FrontendConfig.
这是我的web.xml和FrontendConfig。
Please be indulgent with me. 请放纵我。 Im a newbie :)
我是新手:)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
de.wiegand.mytransport.frontend.config.FrontendConfig
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
de.wiegand.mytransport.frontend.config.FrontendConfig
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Configuration class 配置类
@Import({ StackEnvironment.class })
@EnableWebMvc
@ComponentScan({ "de.wiegand" })
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Configuration
@ImportResource({ "/WEB-INF/spring-security.xml", "/WEB-INF/springsecurity.taglib.xml" })
public class FrontendConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 2DO: Wrong ??????
registry.addResourceHandler("/assets/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(31556926);
registry.addResourceHandler("/css/**").addResourceLocations("/css/")
.setCachePeriod(31556926);
registry.addResourceHandler("/img/**").addResourceLocations("/img/")
.setCachePeriod(31556926);
registry.addResourceHandler("/js/**").addResourceLocations("/js/")
.setCachePeriod(31556926);
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/pages/"); //
resolver.setSuffix(".xhtml");
return resolver;
}
}
THX 谢谢
You've declared the same @Configuration
class 您已经声明了相同的
@Configuration
类
de.wiegand.mytransport.frontend.config.FrontendConfig
in both the ContextLoaderListener
and the DispatcherServlet
. 在
ContextLoaderListener
和DispatcherServlet
。
Both of these create an ApplicationContext
. 这两个都创建一个
ApplicationContext
。 Since the DispatcherServlet
context (child) has access to the beans in the ContextLoaderListener
context (parent), you'll end up with duplicate beans or even initialization errors (depending on the config). 由于
DispatcherServlet
上下文(子级)可以访问ContextLoaderListener
上下文(父级)中的bean,因此最终将出现重复的bean甚至初始化错误(取决于配置)。
Your FrontendConfig
is a WebMvcConfigurerAdapter
and should therefore be the context that the DisaptcherServlet
loads. 您的
FrontendConfig
是一个WebMvcConfigurerAdapter
,因此应该是DisaptcherServlet
加载的上下文。
If you have no other context (application-wide contxt), remove your ContextLoaderListener
declaration in the web.xml
. 如果没有其他上下文(应用程序范围的contxt),请在
web.xml
删除ContextLoaderListener
声明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.