一、注解实现java配置代替xml配置
1 @Configuration:定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。比如:
@Configuration public class MainAutoConfigure { @Bean public TestService1 testService1(){ return new TestService1(); } }
相当于定义了一个applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="testService1" class="TestService1"/> </beans>
2 @Import(AutoConfigureImport.class):引入一个配置类,相当于applicationContext.xml中的
<import resource="autoConfigureImport.xml"/>
二、SPI机制告诉spring boot,java配置类路径
SPI ,全称为 Service Provider Interface,是一种服务发现机制。它通过在ClassPath路径下的META-INF/services文件夹查找文件,自动加载文件里所定义的类。
在Spring中也有一种类似与Java SPI的加载机制。它在META-INF/spring.factories文件中配置接口的实现类名称,然后在程序中读取这些配置文件并实例化。
这种自定义的SPI机制是Spring Boot Starter实现的基础。# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.test.autoconfigure.MainAutoConfigure
三、示例:myTest-spring-boot-starter
1 新建一个工程:myService,提供最简单的TestService服务,和spring没有一毛钱的关系
public class TestService1{
public String sayHi() {
return "hi TestService1!";
}
}
public class TestService2 {
public String sayHi() {
return "hi TestService2!";
}
}
2 再新建一个工程:myTest-spring-boot-starter,用来配置myService工程中的TestService服务
- pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.5.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>myService</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 配置类1:主配置,模拟applicationContext.xml
@Configuration
@Import(AutoConfigureImport.class)
public class MainAutoConfigure {
@Bean
public TestService1 testService1(){
return new TestService1();
}
}
- 配置类2:模拟applicationContext.xml中的import标签
@Configuration
public class AutoConfigureImport {
@Bean
public TestService2 testService2(){
return new TestService2();
}
}
- 新建resources/META-INF/spring.factories,让spring boot自动导入配置类
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.test.autoconfigure.MainAutoConfigure
3 最后新建一个工程:testProject,用来测试myTest-spring-boot-starter:
- pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.test</groupId>
<artifactId>myTest-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
- 测试controller
@RestController
public class TestController {
@Autowired
private TestService1 testService1;
@Autowired
private TestService2 testService2;
@GetMapping("/testService1")
public String testService1(){
return testService1.sayHi();
}
@GetMapping("/testService2")
public String testService2(){
return testService2.sayHi();
}
}
- 启动类:
@SpringBootApplication
public class StartApp {
public static void main(String[] args) {
SpringApplication.run(StartApp.class,args);
}
}
- 测试成功,如下: