003-SpringBoot导入xml配置

SpringBoot理念就是零配置编程,但是如果绝对需要使用XML的配置,我们建议您仍旧从一个@Configuration类开始,你可以使用@ImportResouce注解加载XML配置文件,我拿一个例子来进行讲解:

1、增加一个Springboot无法扫描的类用作测试使用,根据Spring Boot扫描(根包到子包的原则)

@Service
public class TestService {
    public TestService() {
        System.out.println("TestService");
    }
}

2、增加xml配置文件

在src/main/resouces下编写配置文件application-init.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean class="com.jd.bt.TestService"></bean>
</beans>

3、增加配置类ConfigClass

确保能被Spring Boot可以扫描到

package com.jd.bt.gateway.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration

@ImportResource(locations={"classpath:application-init.xml"})
public class ApplicationConfiguration {
}

启动即可

注意:

  ImportResouce有两种常用的引入方式:classpath和file,具体查看如下的例子:

        classpath路径:locations={"classpath:application-init.xml","classpath:application-init2.xml"}

        file路径:locations= {"file:d:/test/application-init1.xml"};

2、

原文地址:https://www.cnblogs.com/bjlhx/p/9039608.html