自己的spring boot starter
这篇文章说的更加详细具体:https://www.cnblogs.com/hjwublog/p/10332042.html
在刚开始看spring boot的时候,发现这么多starter,不免觉得好奇:这些究竟是怎么工作的,每个里边源码都长啥样?今天我们手写一个starter来了解这个过程:
----talk--is--cheap--just--show--you--the--code---------------------------------------------------
首先建一个maven项目:
pom里添加如下内容:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- <version>2.0.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- </dependencies>
属性配置类:
主要是从application.yml中取值,若不设置,默认为hell.msg = world
- @ConfigurationProperties(prefix="hello")
- public class HelloServiceProperties {
- private static final String MSG = "world";
- private String msg = MSG;
- public String getMsg() {
- return msg;
- }
- public void setMsg(String msg) {
- this.msg = msg;
- }
- }
辅助功能类:
本例中依据此类是否存在来创建这个类的bean
- public class HelloService {
- private String msg;
- public String sayHello(){
- return "Hello " + msg;
- }
- public String getMsg() {
- return msg;
- }
- public void setMsg(String msg) {
- this.msg = msg;
- }
- }
自动配置类:
- @Configuration
- @EnableConfigurationProperties(HelloServiceProperties.class)
- @ConditionalOnClass(HelloService.class)
- @ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing = true)
- public class HelloServiceAutoConfiguration {
- @Autowired
- private HelloServiceProperties helloServiceProperties;
- @Bean
- @ConditionalOnMissingBean(HelloService.class)
- public HelloService helloService(){
- HelloService helloService = new HelloService();
- helloService.setMsg(helloServiceProperties.getMsg());
- return helloService;
- }
- }
几个注解有必要解释一下:
@EnableConfigurationProperties(HelloServiceProperties.class) 开启读取文件然后自动配置属性
@ConditionalOnClass(HelloService.class) 类路径中包含某类
@ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing = true) 配置文件hello.enabled没有时,也继续加载,而不是报错
注册配置:
若想自动配置生效,需要注册自动配置类,在src/main/resources下新建META-INF/spring.factories,文件夹没有的话自己手动创建即可,里面内容如下:
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.wisely.spring_boot_starter_hello.HelloServiceAutoConfiguration
若有多个自动配置,则用逗号(,)隔开,此处“\”是为了换行后仍能读到属性。
测试我们的starter:
刚才的程序,我们在本地mvn install一下,将其安装到本地的.m2仓库中,然后新创建spring boot项目,在pom文件中引入我们的starter:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>com.wisely</groupId>
- <artifactId>spring-boot-starter-hello</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
然后就是使用啦:
- @RequestMapping("/startertest")
- @ComponentScan("com.wisely.spring_boot_starter_hello")
- @RestController
- public class MyStarterTestController {
- @Autowired
- HelloService helloService;
- @RequestMapping("/test")
- public String index(){
- return helloService.sayHello();
- }
- }
在application.yml中如下配置:
运行程序,访问,可以看到结果:
可以看到,程序读取了application.yml中的值并进行了展示
--------------------------------------------------------
over
自己的spring boot starter的更多相关文章
- Spring Boot Starter 介绍
http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...
- spring -boot s-tarter 详解
Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- SpringBoot 之Spring Boot Starter依赖包及作用
Spring Boot 之Spring Boot Starter依赖包及作用 spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. spri ...
- Spring boot starter pom的依赖关系说明
Spring Boot 通过starter依赖为项目的依赖管理提供帮助.starter依赖起始就是特殊的maven依赖,利用了传递依赖解析,把常用库聚合在一起,组成了几个为特定功能而定制的依赖. sp ...
- Spring Boot Starter列表
转自:http://blog.sina.com.cn/s/blog_798f713f0102wiy5.html Spring Boot Starter 基本的一共有43种,具体如下: 1)spring ...
- 创建自己的Spring Boot Starter
抽取通用模块作为项目的一个spring boot starter.可参照mybatis的写法. IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入sp ...
- 自己写spring boot starter
自己写spring boot starter 学习了:<spring boot实战>汪云飞著 6.5.4节 pom.xml <project xmlns="http://m ...
- 自定义的Spring Boot starter如何设置自动配置注解
本文首发于个人网站: 在Spring Boot实战之定制自己的starter一文最后提到,触发Spring Boot的配置过程有两种方法: spring.factories:由Spring Boot触 ...
- 手把手教你定制标准Spring Boot starter,真的很清晰
写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...
随机推荐
- PostgreSQL 设置主键的序列值
1. 问题的提出 PostgreSQL定义TABLE时,主键的字段类型可以设定为自增类型serial,即插入每条记录时,主键的值自动加1.但是,当插入数据的时候指定了具体的主键值,例如主键值从0到50 ...
- framwork maven的配置及使用
maven的配置及使用 一.什么是maven: 我们在开发项目的过程中,会使用一些开源框架.第三方的工具等等,这些都是以jar包的方式被项目所引用,并且有些jar包还会依赖其他的jar包,我们同样需要 ...
- c++滚动数组
说来惭愧,我老早以前就学习了dp,可直到最近才知道滚动数组. 所以说,滚动数组是什么呢? 它是一种优化dp空间复杂度的思想. 在dp转移时,我们往往不需要之前推的所有的,而是只需要前一两个转移的. 我 ...
- 【转】 robotframework(rf)中对时间操作的datetime库常用关键字
转自http://blog.csdn.net/r455678/article/details/52993765 DateTime库是robotframework内置的库 1.对固定日期进行操作,增加或 ...
- JavaWeb中MVC的使用--以管理系统举例
开发环境:JavaSE1.7.JavaEE7.0.JSTL1.2.2.Web2.3.MySQL5.5.28 系统分析与功能设计: 本系统实现商品信息的管理,应包括以下几个功能: 商品信息列表:列出所有 ...
- LINQ和Lambda表达式
前言 前段时间接触了一种新的表达式,但是不知道这个是什么意思,所以就先站在巨人的肩膀用了,现在听师哥说这种写法是Lambda表达式.我一直以为,这个Lambda表达式和LINQ查询有异曲同工之妙,可惜 ...
- BS总结篇
学习Web开发差不多三个月了,这个阶段的学习给自己带来的更多的是视觉的盛宴.从CS的世界一下子来到了BS的地盘,心中除了惊喜还是惊喜.在这里还是希望自己对这三月所学的东西做一个阶段性的总结. 话不多说 ...
- apache2.4.X虚拟主机配置
1,用记事本打开apache目录下httpd文件(如:D:\wamp\bin\apache\apache2.2.8\conf),找到如下模块 # Virtual hosts #In ...
- Java Web之数据库连接池
数据库连接池 一.数据库连接池 1. 数据库连接池就是存放数据库连接(Connection)的集合 2. 我们获取一个数据库连接是一个相对很麻烦的过程,如果我们获取一个数据库连接,使用一次以后就给它关 ...
- P2700 逐个击破
题意:现在有N个城市,其中K个被敌方军团占领了,N个城市间有N-1条公路相连,破坏其中某条公路的代价是已知的, 现在,告诉你K个敌方军团所在的城市,以及所有公路破坏的代价,请你算出花费最少的代价将这K ...