以web模块进行分析,引入web的启动器
点spring-boot-starter-web
,再点spring-boot-starter
可以发现,依赖spring-boot-autoconfigure
。也就是说,web的启动器spring-boot-starter-web
是依靠spring-boot-autoconfigure
的
启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,要专门来写一个自动配置模块,启动器依赖自动配置模块,项目中引入相应的starter就会引入启动器的所有传递依赖。
官方命名
spring-boot-starter-模块名
eg:spring-boot-starter-web、spring-boot-starter-jdbc、spring-boot-starter-thymeleaf
自定义命名
模块名-spring-boot-starter
eg:mybatis-spring-boot-start
@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie //结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效,加入到容器中
public class XxxxAutoConfiguration {
自动配置类要能加载,需要将启动就加载的自动配置类配置在META-INF/spring.factories中
比如:
#Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
org.mybatis.spring.boot.autoconfigure.MybatisLanguageDriverAutoConfiguration,
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
<?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.ewen</groupId>
<artifactId>ewen-spring-boot-starter-autoconfiguer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ewen-spring-boot-starter-autoconfiguer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-starter,所有的启动器都要引入这个依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
package com.ewen.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;
}
}
HelloPropertise
@ConfigurationProperties(prefix="ewen.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;
}
}
HelloServiceAutoConfiguration
@Configuration //表明这个类是配置类
@ConditionalOnWebApplication//必须是web应用
@EnableConfigurationProperties(HelloProperties.class)//将HelloProperties类生效并注入到容器中
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
//将helloProperties注入到helloService中
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ewen.starter.HelloServiceAutoConfiguration
<?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.ewne</groupId>
<artifactId>ewen-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入starter的自动配置类-->
<dependencies>
<dependency>
<groupId>com.ewen</groupId>
<artifactId>ewen-spring-boot-starter-autoconfiguer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/hello")
public String sayHello() {
String s = helloService.sayHello("ewen");
return s;
}
}
ewen.hello.prefix=我是
ewen.hello.suffix=o