@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
自动配置类要能加载
将需要启动就加载的自动配置类,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
  • 启动器只用来做依赖导入;
  • 专门来写一个自动配置模块;
  • 启动器依赖自动配置;别人只需要引入启动器(starter)
  • mybatis-spring-boot-starter;自定义启动器名-spring-boot-starter

步骤:

  启动器模块(就是一个Maven项目):

<?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>ustc-anmin-starter</groupId>
<artifactId>ustc-anmin-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties> <!--引入自动配置模块-->
<dependencies>
<dependency>
<groupId>ustc.anmin.starter</groupId>
<artifactId>anmin-spring-boot-starter-autoconfigure</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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ustc.anmin.starter</groupId>
<artifactId>anmin-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>anmin-spring-boot-starter-autoconfigure</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 ustc.anmin.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "ustc.anmin")
public class HelloServiceProperty {
private String prefix;
private String suffix; public String getSuffix() {
return suffix;
} public void setSuffix(String suffix) {
this.suffix = suffix;
} public String getPrefix() {
return prefix;
} public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
package ustc.anmin.starter;

public class HelloService {
HelloServiceProperty helloServiceProperty; public String sayHelloAnmin(String name){
return helloServiceProperty.getPrefix() + name + helloServiceProperty.getSuffix();
} public HelloServiceProperty getHelloServiceProperty() {
return helloServiceProperty;
} public void setHelloServiceProperty(HelloServiceProperty helloServiceProperty) {
this.helloServiceProperty = helloServiceProperty;
}
}
package ustc.anmin.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; @Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloServiceProperty.class) //将配置类加入容器
public class HelloServiceAutoConfiguration {
@Autowired
HelloServiceProperty helloServiceProperty; @Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloServiceProperty(helloServiceProperty);
return new HelloService();
}
}

spring.factories文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
ustc.anmin.starter.HelloServiceAutoConfiguration

test controller

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ustc.anmin</groupId>
<artifactId>starter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>starter-test</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-web</artifactId>
</dependency> <dependency>
<groupId>ustc.anmin.starter</groupId>
<artifactId>ustc-anmin-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
package ustc.anmin.starter.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ustc.anmin.starter.HelloService; @RestController
public class HelloController { @Autowired
HelloService service; @GetMapping("/hello")
public String hello(){
return service.sayHelloAnmin("---anmin---");
} }

最新文章

  1. servlet应用及知识点总结
  2. mate标签
  3. testng 教程之使用参数的一些tricks配合使用reportng
  4. 从官方下载 Bootstrap 版本 并写 第一个页面
  5. ajax查询数据返回结果不变
  6. devm_kzalloc and kmalloc
  7. How Many Shortest Path
  8. Linux学习计划
  9. 你以为你了解最常用的string.substring()的几个常见问题吗?
  10. A Game of Thrones(3) - Daenerys
  11. LeetCode OJ 292.Nim Gam148. Sort List
  12. 技术方案:在外部网址调试本地js(基于fiddler)
  13. 提高java编程质量 - (四)i++ 和 ++i 探究原理
  14. nyoj 寻找最大数(二)
  15. [GitHub]第八讲:GitHub Pages
  16. Java中创建线程的三种方式及其优缺点
  17. 在Docker中体验数据库之Microsoft SQL Server
  18. 利用 html js判断 客户端是否安装了某个app 安装了就打开 否则跳转到gp
  19. Java中的属性和方法
  20. mysql中的几种日志了解

热门文章

  1. jmeter+jenkins+ant部署持续集成测试
  2. XMPP即时通讯基础知识
  3. vim 退出命令(保存、放弃保存)
  4. python学习笔记2-条件语句
  5. iView 实战系列教程(21课时)_1.iView 实战教程之配置篇
  6. JS中的回调函数实例浅析
  7. 家庭wifi,如何组网最合适
  8. 【弱的C艹之路。。未完待续】
  9. loj#2542. 「PKUWC2018」随机游走(树形dp+Min-Max容斥)
  10. CodeForces - 816C Karen and Game(简单模拟)