@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---");
} }

SpringBoot自定义一个starter的更多相关文章

  1. Spring boot 自定义一个starter pom

    用过springboot的自动配置会觉得非常方便,我们完全可以自己写一个starter pom,这样不仅可以有自动配置功能,而且具有更通用的的耦合度低的配置, 新建一个starter的maven项目, ...

  2. SpringBoot编写自定义的starter 专题

    What’s in a name All official starters follow a similar naming pattern; spring-boot-starter-*, where ...

  3. SpringBoot系列三:SpringBoot自定义Starter

    在前面两章 SpringBoot入门 .SpringBoot自动配置原理 的学习后,我们对如何创建一个 SpringBoot 项目.SpringBoot 的运行原理以及自动配置等都有了一定的了解.如果 ...

  4. springboot系列十四、自定义实现starter

    一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...

  5. springboot 自定义starter之AutoConfiguration【原】

    八.自定义starter AutoConfiguration: 1.这个场景需要使用到的依赖是什么? 没有特别依赖的配置 2.如何编写自动配置 @Configuration //指定这个类是一个配置类 ...

  6. SpringBoot --- 自定义 Starter

    SpringBoot --- 自定义 Starter 创建 1.需要创建一个新的空工程 2.新的工程需要引入两个模块 一个Maven 模块 作为启动器 一个SpringBoot 模块 作为自动配置模块 ...

  7. springBoot 自动配置原理--自己新建一个 starter

    上篇我们说到 springboot 和 SSM 框架的区别,今天我们就看看 springboot 到底为我们做了哪些事情,让我们开发变得如此简单. springboot 中起着重要作用的是 start ...

  8. 深入springboot原理——动手封装一个starter

    从上一篇文章<深入springboot原理——一步步分析springboot启动机制(starter机制)> 我们已经知道springboot的起步依赖与自动配置的机制.spring-bo ...

  9. springboot2.x基础教程:动手制作一个starter包

    上一篇博客介绍了springboot自动装配的原理.springboot本身有丰富的spring-boot-starter-xx集成组件,这一篇趁热打铁加深理解,我们利用springboot自动装配的 ...

随机推荐

  1. ASP.NET Core:Pages

    ylbtech-ASP.NET Core:Pages 1.返回顶部 1._Layout.cshtm <!DOCTYPE html> <html> <head> &l ...

  2. web服务端的架构演变

    此文已由作者肖凡授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近Lofter项目碰到很多性能上的问题,特别是数据库相关的,每次推送后,告警就会第一时间到来.这些问题随着产 ...

  3. ACM中java的使用

    ACM中java的使用 转载自http://www.cnblogs.com/XBWer/archive/2012/06/24/2560532.html 这里指的java速成,只限于java语法,包括输 ...

  4. SCUT - 243 - 宝华复习 - 二分 - 桶计数

    https://scut.online/p/243 这道题唯一难点在于如何快速确定m合法.可以统计滑动窗口中已有元素的数量. #include<bits/stdc++.h> using n ...

  5. Vue 基础指令学习

    学习Vue的一些总结,第一次写博客,文笔实在是很差 不过我会不断写的. <template> <!--只能有一个根节点 --> <div> <pre> ...

  6. 第三篇 .NET高级技术之深拷贝和浅拷贝

    深拷贝.浅拷贝 如果拷贝的时候共享被引用的对象就是浅拷贝,如果被引用的对象也拷贝一份出来就是深拷贝.(深拷贝就是说重新new一个对象,然后把之前的那个对象的属性值在重新赋值给这个用户) using S ...

  7. UML 图中类之间的关系:依赖,泛化,关联,聚合,组合,实现

    http://www.uml.org.cn/oobject/201211231.asp 1. 关联 一个类作为另外一个类的变量:在UML类图中,用实线连接有关联关系的对象所对应的类 2.聚合 表示整体 ...

  8. java webRoot 路径问题

    项目部署后的目录结构 src 生成到 WEB-INF\classes文件下; WebRoot  为项目的根目录,应用中“/action”就相当于是系统目录中的”WebRoot/action" ...

  9. 使用c++的一些建议

    1: 不要使用宏,用const或enum定义常量 用inline避免函数的额外调用(使用inline的函数,块里面尽量不要使用循环和递归) 用template去荷花一些函数或者类型 用namespac ...

  10. A.华华听月月唱歌

    链接:https://ac.nowcoder.com/acm/contest/392/A 题意: 月月唱歌超级好听的说!华华听说月月在某个网站发布了自己唱的歌曲,于是把完整的歌曲下载到了U盘里.然而华 ...