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>