说明

在使用非spring boot项目我们集成spring mvc mybatis等框架往往需要大量xml配置, spring 的推出是为了解决项目的复杂度,随着项目的增长,xml配置会越来越臃肿,

所以spring 推出了spring boot 使用自动化配置 通过javebean的配置方式为我们配置常用的配置方式,如果有定制需求 可以覆盖默认配置。实现方式则是根据Starter

自己实现一个Starter

Spring 官方 Starter通常命名为 spring-boot-starter-{name} 如 spring-boot-starter-web 

Spring官方建议非官方Starter命名应遵循 {name}-spring-boot-starter 的格式

创建一个项目

2.引入pom依赖

<dependencies>
<!-- @ConfigurationProperties annotation processing (metadata for IDEs)
生成spring-configuration-metadata.json类,需要引入此类 主要用于ideal 通过点击yml或者properties属性跳转到你的配置类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>

创建配置类

@ConfigurationProperties("example.service")
public class TestProperties {
private String name;
private String url; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getUrl() {
return url;
} public void setUrl(String url) {
this.url = url;
}
}

里面还可以配置对象或者map

通过example.service.[mapObjectName].[keyName]=valie

通过example.service.[ObjectName].[filedName]=valie

创建Service类

public class TestService {
private TestProperties testProperties; public TestService(TestProperties testProperties){
this.testProperties=testProperties;
}
}

创建配置类

@Configuration
@ConditionalOnClass(TestService.class)//当Spring Context中不存在该Bean时。
@EnableConfigurationProperties(TestProperties.class)
public class TestAutoConfigure { private final TestProperties properties; @Autowired
public TestAutoConfigure(TestProperties properties) {
this.properties = properties;
} @Bean
@ConditionalOnMissingBean//当classpath下发现该类的情况下进行自动配置。
@ConditionalOnProperty(prefix = "test.service", value = "enabled",havingValue = "true")//当配置文件中example.service.enabled=true时。
TestService exampleService (){
return new TestService(properties);
}
}

创建spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
test.teststarter.TestAutoConfigure

如果多个配置类,号隔开

更多Conditional配置

@ConditionalOnBean:当容器中有指定的Bean的条件下
@ConditionalOnClass:当类路径下有指定的类的条件下
@ConditionalOnExpression:基于SpEL表达式作为判断条件
@ConditionalOnJava:基于JVM版本作为判断条件
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置
@ConditionalOnMissingBean:当容器中没有指定Bean的情况下
@ConditionalOnMissingClass:当类路径下没有指定的类的条件下
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下
@ConditionalOnProperty:指定的属性是否有指定的值
@ConditionalOnResource:类路径下是否有指定的资源
@ConditionalOnSingleCandidate:当指定的Bean在容器中只有一个,或者在有多个Bean的情况下,用来指定首选的Bean @ConditionalOnWebApplication:当前项目是Web项目的条件下

测试

将项目打包 另外一个项目依赖并配置

1.配置

example:
service:
name: "dd"
url: "ccc"
test:
service:
enabled: true

2.运行unit test

@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisSourceTestApplicationTests {
@Autowired
TestService testService; @Test
public void contextLoads() {
System.out.print(testService.toString());
} }

Spring Boot-Starter(九)的更多相关文章

  1. Spring Boot Starter 介绍

    http://www.baeldung.com/spring-boot-starters 作者:baeldung 译者:http://oopsguy.com 1.概述 依赖管理是任何复杂项目的关键部分 ...

  2. spring -boot s-tarter 详解

    Starter POMs是可以包含到应用中的一个方便的依赖关系描述符集合.你可以获取所有Spring及相关技术的一站式服务,而不需要翻阅示例代码,拷贝粘贴大量的依赖描述符.例如,如果你想使用Sprin ...

  3. spring boot / cloud (九) 使用rabbitmq消息中间件

    spring boot / cloud (九) 使用rabbitmq消息中间件 前言 rabbitmq介绍: RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.它可以用于大型软件系统 ...

  4. Spring Boot (一): Spring Boot starter自定义

    前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...

  5. spring Boot(十九):使用Spring Boot Actuator监控应用

    spring Boot(十九):使用Spring Boot Actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台 ...

  6. SpringBoot 之Spring Boot Starter依赖包及作用

    Spring Boot 之Spring Boot Starter依赖包及作用 spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. spri ...

  7. Spring boot starter pom的依赖关系说明

    Spring Boot 通过starter依赖为项目的依赖管理提供帮助.starter依赖起始就是特殊的maven依赖,利用了传递依赖解析,把常用库聚合在一起,组成了几个为特定功能而定制的依赖. sp ...

  8. Spring Boot Starter列表

    转自:http://blog.sina.com.cn/s/blog_798f713f0102wiy5.html Spring Boot Starter 基本的一共有43种,具体如下: 1)spring ...

  9. 创建自己的Spring Boot Starter

    抽取通用模块作为项目的一个spring boot starter.可参照mybatis的写法. IDEA创建Empty Project并添加如下2个module,一个基本maven模块,另一个引入sp ...

  10. 自己写spring boot starter

    自己写spring boot starter 学习了:<spring boot实战>汪云飞著 6.5.4节 pom.xml <project xmlns="http://m ...

随机推荐

  1. anaconda安装python三方包,以tensorflow为例

    Anaconda概述 Anaconda是一个用于科学计算的Python发行版,支持 Linux, Mac, Windows系统,提供了包管理与环境管理的功能,可以很方便地解决多版本python并存.切 ...

  2. 【HAOI 2008】 糖果传递

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1045 [算法] 环形均分纸牌问题 [代码] #include<bits/std ...

  3. Luogu3403跳楼机

    https://zybuluo.com/ysner/note/1099616 题面 给你三个数\(x\),\(y\),\(z\),问你能够凑出多少个[1,\(h\)]之间的数. 解析 处理出\(y\) ...

  4. 58. Extjs grid CheckboxSelectionModel 单选

    转自:https://kabuka.iteye.com/blog/581823 默认的 new Ext.grid.CheckboxSelectionModel 都是可以多选的,但是有时候需要控制单选时 ...

  5. 软RAID管理

    软RAID管理 软RAID 软RAID 提供管理界面:mdadm 软RAID为空余磁盘添加冗余,结合了内核中的md(multi devices). RAID 设备可命名为/dev/md0./dev/m ...

  6. 9.15NOIP模拟题

    GRYZ 模拟考试套题 9.15 gryz信息组专场 题目名称 最初的最初 太 妃 糖 可执行文件名 eat hwc dance sugar 输入文件 eat.in hwc.in dance.in s ...

  7. P3187 [HNOI2007]最小矩形覆盖

    传送门 首先这个矩形的一条边肯定在凸包上.那么可以求出凸包然后枚举边,用类似旋转卡壳的方法求出另外三条边的位置,也就是求出以它为底最上面最右边最左边的点的位置.离它最远的点可以用叉积求,最左最右的可以 ...

  8. mariadb的安装

    mysql (分支 mariadb)1.安装mariadb -yum -源码编译安装 -下载rpm安装 yum和源码编译安装的区别? 1.路径区别-yum安装的软件是他自定义的,源码安装的软件./co ...

  9. MyBatis 配置控制台上显示sql语句(log4j.properties 之三)

    ### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.app ...

  10. css3 绘制书本

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...