【问题标题】:importing XML config into java config based (annotation) project将 XML 配置导入基于 Java 配置(注释)的项目
【发布时间】:2016-08-02 22:51:33
【问题描述】:

我正在开发基于 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:没有定义名为“springSecurityFilterChain”的bean 在 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 {

截图

http://i.stack.imgur.com/BosWG.jpg

【问题讨论】:

  • 你可以看看这个问题:stackoverflow.com/questions/24014919/… TL;DR: use @ImportResource
  • 你找到解决方案了吗,因为我有同样的问题。我的文件在WEB-INF/spring/security-context.xml 下谢谢

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


【解决方案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 目录并使用使用类路径加载文件的文件行。这些文件将在 maven 打包战争时复制到 WEB-INF/classes 目录,即类路径

【讨论】:

  • 我已经尝试过您建议的方式,但对我没有任何效果。请注意,我已将 securityContext.xml 文件保存在 WEB-INF 文件夹中(在 web.xml 级别)
  • 你试过@ImportResource("/WEB-INF/securityContext.xml")
  • 是的。我尝试了所有可能的方法来通过 @ ImportResources 添加资源,但没有任何效果。请查找项目结构的图片。
  • 添加这个@ImportResource("file:**/WEB-INF/securityContext.xml") 就可以了
  • 您执行了哪一步?文件内容或按照我的建议将文件移动到 src/main/resources 目录
猜你喜欢
  • 2010-09-15
  • 2017-05-27
  • 2012-01-15
  • 2015-07-11
  • 1970-01-01
  • 2014-09-25
  • 2012-02-01
  • 2013-03-13
  • 1970-01-01
相关资源
最近更新 更多