SpringBoot23 之 导入XML配置

来源:互联网 发布:飞扬软件官网 编辑:程序博客网 时间:2024/06/05 11:20

1.新建一个maven工程
2.创建Springboot项目
3创建包
4创建Service

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

这个例子的大体步骤如下:
(1)新建一个工程;
(2)在App.java类编写HelloService2;
(3)在App.java类无法扫描的包下编写HelloService;
(4)编写application-bean.xml注入HelloService;
(5)编写ConfigClass注入配置文件application-bean.xml;
(6)编写App.java启动类进行测试;
(7)其它说明

@Servicepublic class HelloService {    public  HelloService(){        System.out.print("helloServiceto1");    }}@Servicepublic class HelloService2 {    public  HelloService2(){        System.out.print("helloServiceto2");    }}

5.创建配置类

@Configuration@ImportResource(locations={"classpath:application-bean.xml"})public class config {}

配置文件application-bean.xml在src/main/resource下

<?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 id="helloService" class="com.test.service.HelloService"></bean></beans>
0 0
原创粉丝点击