Spring Boot 导入Xml配置

本文介绍了如何在Spring Boot应用中引入XML配置,通过@ImportResource注解加载配置文件,结合Service和Controller展示了XML配置的工作流程。启动类加载beans.xml后,实现了HelloService的调用。
摘要由CSDN通过智能技术生成

测试类:
Service类:

public class HelloService {
    public String gethello(){
        return "hello xml";
    }
}

Controller类:

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping("/hello")
    public void getHello(){
        System.out.println(helloService.gethello());
    }
}

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.dong.xml.HelloService" id="HelloController"/>
</beans>

至关重要的是启动类,只需要在启动类上加上@ImportResource注解,并标明位置,就成功配置了

@SpringBootApplication
@ImportResource("classpath:beans.xml")
public class XmlApplication {

    public static void main(String[] args) {
        SpringApplication.run(XmlApplication.class, args);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值