stater:
这个场景需要用到的依赖是什么?
如何编写自动配置?
@Configuration//指定这个类是一个配置类
@ConditionalOnxxx//在指定条件成立的情况下,自动配置类生效
@AutoConfiureAfter//指定自动配置类的相对顺序
@Bean//给容器中添加组件
@EnableConfigurationProperties//结合相关xxProperties类来绑定相关的配置
第一个为Maven模块
第二个为SpringInitializer模块
将autoconfig的模块坐标放入到starter的依赖文件中
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ezerbelcn.starter</groupId>
<artifactId>ezerbelcn-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!--启动器依赖-->
<dependencies>
<!--引入自动配置模块-->
<dependency>
<groupId>com.ezerbelcn.starter</groupId>
<artifactId>ezerbelcn-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ezerbelcn.starter</groupId>
<artifactId>ezerbelcn-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ezerbelcn-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
package com.ezerbelcn.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "ezerbelcn.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
package com.ezerbelcn.starter;
public class HelloService {
HelloProperties helloProperties;
public String sayHello(String name){
return helloProperties.getPrefix() + name + helloProperties.getSuffix();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
package com.ezerbelcn.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnWebApplication//web应用才会生效
@Configuration
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
<!--引入自定义的starter-->
<dependency>
<groupId>com.ezerbelcn.starter</groupId>
<artifactId>ezerbelcn-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
starter和stater的相应依赖都被引入进来了
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String helllo(){
return helloService.sayHello("Lilith");
}
}
总结流程:
starter
作用:仅仅指定项目所需的各种依赖即可
命名:xxx-spring-boot-starter
auto-configure
作用:
提供自动配置类,内部包含@Bean方法以及其他装配方式,通常可以结合properties类读取配置文件内的信息。
通常使用@EnableConfigurationProperties(xxxProperties.class)来标注这个配置类。
xxxProperties.class 类结合@ConfigurationProperties(prefix=“xxx.xxx”)来指明配置文件中对应信息的前缀。
命名:xxx-spring-boot-starter-autoconfigure