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 ...
随机推荐
- poj3539 Elevator——同余类bfs
题目:http://poj.org/problem?id=3539 题目大意是给定 a, b, c,求 1~h 内有多少个数可以被 a, b, c 通过加减法组成: 这是今天刚讲的神奇的——同余类 b ...
- openstack service glance-api/registry mysql of max_connection
- Gym - 101972B Arabella Collegiate Programming Contest (2018) B. Updating the Tree 树DFS
题面 题意:T组数据,每次给你1e5个点的树(1为根),每个点有一权值,询问1-n每个节点的子树中, 至少修改几个点的权值(每次都可以任意修改),才能让子树中任意2点的距离==他们权值差的绝对值 无解 ...
- Tornado异步模式
先介绍下背景:由于工作需要,前段时间又写了一段爬虫去获取和更新一些数据.之前爬虫主要用Scrapy框架批量爬取一些页面数据,或者用Gevent调用目标站点的接口.偶然看到了Tornado,听说这个框架 ...
- vue---思维导图
持续更新啦啦啦啦
- [转]mysql的约束
转自:http://blog.csdn.net/kqygww/article/details/8882990 MySQL中约束保存在information_schema数据库的table_constr ...
- No operations allowed after connection closed--转
https://www.jianshu.com/p/1626d41572f2 Spring boot的单数据源配置比较简单,只需要在application.properties配置相关的jdbc连接的 ...
- cocos2dx实现单机版三国杀(二)
接上续,东西还没有做完 所以代码免不了改动 之前的头文件现在又改了不少,因为架构也改变了现在主要类就是GameScene.GameUI.PlayInfo.Poker这四个类 前面想的GameLoop ...
- iOS device is locked/unlocked (判断手机屏幕是否锁屏)
#import <notify.h> -(void)checkDeviceLockScreenState { int notify_token; notify_register_dispa ...
- xampp中localhost与DreamWaver站点设置问题
作为一个初学者,在DreamWaver中配置web服务器用于本地测试,中间碰到了好多问题,百度答案模糊不清,自己摸索出来,把自己碰到的,易错的地方做个总结. step1 : 安装xampp(安装位置记 ...