spring boot遵循“约定优于配置”的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来。spring boot的神奇不是借助代码的生成来实现的,而是通过条件注解来实现的。

  自动配置AutoConfiguration是实现spring boot的重要原理,理解AutoConfiguration的运行原理特别重要,自己写一个AutoConfiguration可以加深我们对spring boot的理解。

1、定义Type-safe Configuration Properties

@ConfigurationProperties("author")//1
public class AuthorPropertis {
private static final String NAME = "pyj"; private static final String PWD = "12324"; private String name = NAME;//2 private String pwd = PWD; private String[] arrayProps;//3 private Properties properties = new Properties();//4 private List<Map<String, Object>> listProp1 = new ArrayList();//5 private List<String> listProp2 = new ArrayList();//6 private Map<String, Object> mapProps = new HashMap();//7 getter and setter...
}

1、@ConfigurationProperties映射application.yml以author为前缀的配置

2、author.name的默认值是pyj

3-7:对于容器对象的属性注入

2、定义一个简单的bean

public class HelloService {

    private String msg;
  getter and setter...
 }

注意:不用纠结HelloService的内容,它就是一个简单的bean。

3、定义AutoConfiguration

@Configuration//1
@ConditionalOnClass(HelloService.class)//2
@EnableConfigurationProperties(AuthorPropertis.class)//3
@ConditionalOnProperty(prefix = "author",value = "enabled",matchIfMissing = true)//4
public class AuthorAutoConfiguration { private final AuthorPropertis authorPropertis; public AuthorAutoConfiguration(AuthorPropertis authorPropertis) {//5
this.authorPropertis = authorPropertis;
} @Bean//6
@ConditionalOnMissingBean(HelloService.class)//7
public HelloService helloService() throws JsonProcessingException {
HelloService helloService = new HelloService();
helloService.setMsg(authorPropertis.getName() +" :" + authorPropertis.getPwd()); ObjectMapper objectMapper = new ObjectMapper();
System.out.println("arrayProps: " + objectMapper.writeValueAsString(authorPropertis.getArrayProps()));
System.out.println("listProp1: " + objectMapper.writeValueAsString(authorPropertis.getListProp1()));
System.out.println("listProp2: " + objectMapper.writeValueAsString(authorPropertis.getListProp2()));
System.out.println("mapProps: " + objectMapper.writeValueAsString(authorPropertis.getMapProps()));
System.out.println("Props: " + objectMapper.writeValueAsString(authorPropertis.getProperties()));
return helloService;
}
}

1、@Configuration声明为一个配置类

2、@ConditionalOnClass表示HelloService在类路径下的时候调用

3、@EnableConfigurationProperties使用AuthorPropertis的配置

4、@ConditionalOnProperty指定的author是否有默认值

5、注入AuthorPropertis

6-7、当不存在HelloService的bean时候,新建一个HelloService的bean

4、在spring.factories注册

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.github.gin.springboot.config.AuthorAutoConfiguration

  在resources新建META-INF目录,再新建spring.factories,注册上AuthorAutoConfiguration,让spring boot知道为你自动配置。

  如果不明白这个的原理,可以看看@EnableAutoConfiguration通过@Import({EnableAutoConfigurationImportSelector.class})来读取spring.factories

5、配置文件application.yml

debug: true

author:
name: pyj
pwd: 1234
properties:
reasonable: 'true'
returnPageInfo: 'true'
listProp1:
- name: abc
value: sgaw
- name: efg
value: sagsag
listProp2:
- config2Value1
- config2Vavlue2
mapProps:
reasonable: true
returnPageInfo: true
arrayProps: 1,2,3,4,5

6、测试

@RestController
public class TestController { @Autowired
HelloService helloService; @RequestMapping("/")
public String index(){
return "hello world!";
} @RequestMapping("/hello")
public String hello(){
return helloService.getMsg();
}
}

运行mvn spring-boot:run,如果看到结果,那么证明是没问题的

  上面的AuthorPropertis 、HelloService 、AuthorAutoConfiguration 可以作为一个独立的starter pom为其他项目提供一个通用的服务。虽然spring boot覆盖了大部分的使用场景,但也不是全部都有,但我们也可以利用AutoConfiguration起到同样的效果。

  实例代码地址:有爱自取

学记:为spring boot写一个自动配置的更多相关文章

  1. 安装使用Spring boot 写一个hello1

    一.创建springboot 项目 二.进行代编写 1.连接数据库:application.properties里配置 spring.datasource.driverClassName=com.my ...

  2. Spring Boot之实现自动配置

    GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...

  3. 4、Spring Boot 2.x 自动配置原理

    1.4 Spring Boot 自动配置原理 简介 spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate ...

  4. Spring Boot面试杀手锏————自动配置原理

    转:https://blog.csdn.net/u014745069/article/details/83820511 引言不论在工作中,亦或是求职面试,Spring Boot已经成为我们必知必会的技 ...

  5. 学记:spring boot使用官网推荐以外的其他数据源druid

    虽然spring boot提供了4种数据源的配置,但是如果要使用其他的数据源怎么办?例如,有人就是喜欢druid可以监控的强大功能,有些人项目的需要使用c3p0,那么,我们就没办法了吗?我们就要编程式 ...

  6. Spring Boot框架的自动配置

    (图片来源于网络,侵删!!!) l  @RestController 因为我们例子是写一个web应用,因此写的这个注解,这个注解相当于同时添加@Controller和@ResponseBody注解 l ...

  7. SpringBoot 源码解析 (五)----- Spring Boot的核心能力 - 自动配置源码解析

    在上一篇博客中分析了springBoot启动流程,大体的轮廓只是冰山一角.今天就来看一下springBoot的亮点功能:自动化装配功能. 先从@SpringBootApplication开始.在启动流 ...

  8. 如何基于Spring Boot搭建一个完整的项目

    前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...

  9. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

随机推荐

  1. solr添加多个core

    在D:\solr\solr_web\solrhome文件夹下: 1)创建core0文件夹 2)复制D:\solr\solr_web\solrhome\configsets\basic_configs/ ...

  2. 记一次git amend事故处理方案

    一.问题回顾 问题是git commit --amend 引起的. 一条commit已经push到远端develop了,但是后来又在这条commit上进行了amend操作,导致这条commit的哈希码 ...

  3. Android开发案例 - 自定义虚拟键盘

    所有包含IM功能的App(如微信, 微博, QQ, 支付宝等)都提供了Emoji表情之类的虚拟键盘,  如下图:    本文只着重介绍如何实现输入法键盘和自定义虚拟键盘的流畅切换, 而不介绍如何实现虚 ...

  4. swift学习笔记3——类、结构体、枚举

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  5. SEED信息安全实验系列:缓冲区溢出漏洞实验

    缓冲区溢出漏洞实验 本课程详细出自http://www.shiyanlou.com/courses/231,转载请注明出处. 一.实验描述 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情 ...

  6. Windows下SVN服务器的搭建步骤

    1.下载svn服务端和客户端 服务端VISUALSVN SERVER:https://www.visualsvn.com/ 客户端TortoiseSVN:https://tortoisesvn.net ...

  7. 容易忘记的git命令

    pwd命令:显示当前的目录 git init:把当前目录变成git可以管理的仓库 git diff 文件名:查看修改了什么内容 git log:查看commit历史,包括时间.作者.版本号.commi ...

  8. SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)

    今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...

  9. SQL语句查数据库中某一列是否有重复项

    Select 列名,COUNT(列名)FROM 表名GROUP BY 列名HAVING COUNT( 列名 ) 〉1

  10. Unity 重要基础知识点

    这是两个月前的学习记录,发出来了下,如果有误欢迎大家指出: 脚本生命周期 //每当脚本被加载时调用一次 // 1.   在Awake中做一些初始化操作 void Awake(){ //初始化publi ...