Spring Boot-Starter(九)
说明
在使用非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(九)的更多相关文章
- 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 / cloud (九) 使用rabbitmq消息中间件
spring boot / cloud (九) 使用rabbitmq消息中间件 前言 rabbitmq介绍: RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.它可以用于大型软件系统 ...
- Spring Boot (一): Spring Boot starter自定义
前些日子在公司接触了spring boot和spring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用.但是由于其自动化配 ...
- spring Boot(十九):使用Spring Boot Actuator监控应用
spring Boot(十九):使用Spring Boot Actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台 ...
- 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 ...
随机推荐
- SpringBoot集成Mybatis配置动态数据源
很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...
- SQL--大解密之组织数据
今天为大家带来的是数据库的基本用法 首先带大家了解下数据库. 大量的数据正在不断产生,伴随而来的是如何安全有效地存储.检索.管理他们. 对数据的有效存储.高效访问.方便共享和安全控制等问题成为信息 ...
- 10.Nodes and Bindings
节点数据绑定 节点是构成Ventuz场景的基本元素.每个节点既属于图层.也属于层级或内容.既可以在图层编辑器,也可以在层级编辑器或内容编辑器中编辑. 内容节点包括资产描述(如材质.xml文件等).数字 ...
- crontab的使用
基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 第4列表示 ...
- Laravel5.1学习笔记20 EloquentORM 关系
Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has ...
- JVM中线程状态转换图
JVM中线程的状态转换图 线程在一定条件下,状态会发生变化.线程一共有以下几种状态: 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该 ...
- python--9、进程及并发知识
进程 一个文件的正在执行.运行过程就成为一个进程.执行多个程序,把程序文件都加载到内存,并且多个程序的内存空间隔离--空间上的复用. 遇到IO等待,切CPU到别的程序,提升效率.没有IO,一个程序占用 ...
- Android彻底组件化demo发布
今年6月份开始,我开始负责对"得到app"的android代码进行组件化拆分,在动手之前我查阅了很多组件化或者模块化的文章,虽然有一些收获,但是很少有文章能够给出一个整体且有效的方 ...
- Gradle的属性Property设置与调用
Gradle在默认情况下已经为Project定义了很多Property: project:Project本身 name:Project的名字 path:Project的绝对路径 description ...
- CxImage实现9PNG
CxImage* ScaleImageBy9PNG(CxImage *pRawImage, int nDstWidth,int nDstHeight) { if(NULL == pRawImage) ...