0

我想使用 Spring 安全性迁移 Spring WebMvcConfigurerAdapter 配置。现在我得到的问题是我得到了 2 个 ApplicationContextes 并且我的 Singelten 类是有状态的。这是我的 web.xml 和我的 FrontendConfig。

请放纵我。我是新手:)

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

配置类

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

谢谢

4

1 回答 1

0

You've declared the same @Configuration class

 de.wiegand.mytransport.frontend.config.FrontendConfig

in both the ContextLoaderListener and the DispatcherServlet.

Both of these create an 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).

Your FrontendConfig is a WebMvcConfigurerAdapter and should therefore be the context that the DisaptcherServlet loads.

If you have no other context (application-wide contxt), remove your ContextLoaderListener declaration in the web.xml.

于 2013-08-31T16:24:45.550 回答