【问题标题】:How to import xml files in Spring Boot?如何在 Spring Boot 中导入 xml 文件?
【发布时间】:2020-02-24 02:53:41
【问题描述】:

我正在将一个 Spring 应用程序移植到 Spring Boot。我希望能够使用 xml 配置 Spring Security,这样我就可以使用我的旧配置文件。 (我尝试过使用 Java 配置,但它不起作用)。

如何让 Spring Boot 获取 xml 配置? Spring 没有看到这方面的任何文档。

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您可以使用org.springframework.context.annotation.ImportResource 注解导入xml 配置。在使用@Configuration 注释的配置类之一中,执行以下操作。

    如果您的安全配置在类路径的根目录中命名为 security-config.xml,则添加以下类似内容。

    @ImportResource({"classpath:security-config.xml"})
    

    【讨论】:

      【解决方案2】:

      您必须使用@ImportResource

      @SpringBootApplication
      @ImportResource("classpath:<name>.xml")
      public class Config {
         public static void main(String[] args) {
            SpringApplication.run(Config.class, args);
         }  
      }
      

      请参阅这些链接以正确理解:

      1. @ImportResource - Java Docs
      2. A well defined example on @ImportResource

      【讨论】:

        【解决方案3】:

        为什么要导入旧的 xml 文件? spring-boot 最大的好处就是不用使用旧文件,可以在java中重写

        【讨论】:

          猜你喜欢
          • 2018-10-15
          • 1970-01-01
          • 2022-11-10
          • 1970-01-01
          • 2020-05-05
          • 2014-08-21
          • 1970-01-01
          • 2021-11-08
          • 2016-06-07
          相关资源
          最近更新 更多