说明

在使用非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. PCB MongoDB 监控

    一个数据库监控工具是必不可少的,当然MongoDB安装自带监控啦. 这里将监控工具mongostat.exe与mongotop.exe使用与参数进行讲解说明. 一.监控工具说明: 二.监控工具启用 1 ...

  2. discuz的全局变量

    $_G 保存了 discuz! 中所有的预处理数据 缓存能够很好的提高程序的性能,一些配置数据没必要每次都查询数据库,只要在修改了的时候更新下缓存即可. Discuz! 中所有的缓存保存在 $_G[c ...

  3. bzoj4247: 挂饰(背包dp)

    4247: 挂饰 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1136  Solved: 454[Submit][Status][Discuss] ...

  4. P3291 [SCOI2016]妖怪

    传送门 我数学的确白学了--这种题目竟然一点思路都没有-- 首先可以把每个妖怪看成二维平面上的一个点,那么每一个环境\((a,b)\)就可以看成一条斜率\(k=-\frac{b}{a}\)的过该点的直 ...

  5. 什么是JavaScript对象?

    对象是JavaScript的基本数据类型.对象是一种复合值:它将很多值(原始值或者其他对象)聚合在一起,可通过名字访问这些值.对象也可看做是属性的无序集合,每个属性都是一个名/值对.属性名是字符串,因 ...

  6. Akka源码分析-Remote-ActorSystem

    前面的文章都是基于local模式分析的,现在我们简要分析一下在remote模式下,ActorSystem的创建过程. final val ProviderClass: String = setup.g ...

  7. 如何写出网页高性能的DOM来提升网页的加载速度

    为什么要写高性能DOM? 一个网站,在页面上承载最多内容的就是DOM,而且无论是我们通过加载JS.加载图片,他们也是通过写HTML标签来实现的.而我们性能优化要做的无非就是几大块: 站点的网络消耗 D ...

  8. java定时器和实时查询数据库

    定时器: Timer timer = new Timer();                    timer.schedule(new TimerTask() {                  ...

  9. Android项目实战_手机安全卫士splash界面

    - 根据代码的类型组织包结构 1. 界面 com.hb.mobilesafe.activities 2. 服务 com.hb.mobilesafe.services 3. 业务逻辑 com.hb.mo ...

  10. JS高级——递归

    基本概念 1.在函数内调用函数自己,就是递归 2.没有递归结束条件的递归,就是死递归 3.化归思想,将一个问题由难化易,由繁化简,由复杂化简单的过程称为化归,它是转化和归结的简称. 基本使用 1.计算 ...