SpringBoot23 之 导入XML配置

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)其它说明

@Service
public class HelloService {
    public  HelloService(){
        System.out.print("helloServiceto1");
    }
}


@Service
public 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值