将XML配置导入基于java配置的(注释)项目

时间:2016-04-12 11:01:56

标签: spring spring-mvc spring-security spring-annotations

我正在使用基于Spring MVC注释的应用程序。我有web.xml文件条目如下(使用WebConfig.java进行配置):

<servlet>
    <servlet-name>sdsdispatcher</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>com.conf.WebConfig</param-value>
    </init-param>            

    <load-on-startup>1</load-on-startup>
</servlet>

现在,当我尝试集成安全相关的XML文件时,我面临以下错误

  

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为&#39; springSecurityFilterChain&#39;被定义为       在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)

我尝试按如下方式导入xml文件:

@Configuration
@EnableWebMvc
@ComponentScan("com.stk.controller")
@ImportResource({"securityContext.xml"})
public class WebConfig extends WebMvcConfigurerAdapter {

截图

enter image description here     http://i.stack.imgur.com/BosWG.jpg

1 个答案:

答案 0 :(得分:1)

添加类路径,如下所示,在类路径中提供

@ImportResource("classpath:securityContext.xml")

如果您有多个配置文件,那么

@ImportResource(locations={"classpath:securityContext.xml","file://c:/test-config.xml"})

您可以使用

从WEB-INF目录访问该文件
@ImportResource("file:**/WEB-INF/securityContext.xml")

但是,我建议您将配置文件移动到src / main / resource目录,并使用使用classpath加载文件的文件行。这些文件将在maven包装战争期间被复制到WEB-INF/classes目录,这是类路径