先说下springboot的运行原理

springboot最主要的配置 是 @SpringBootApplication

然后这里面 @EnableAutoCOnfiguration 最为重要, 继续往里跟

可看到通过 @Import 导入了一个 EnableAutoConfigurationImportSelector 的类

在这个类中, 通过SpringFactoriesLoader.loadFactoryNames方法来扫描所有包下的 spring.factories文件,,, 而在spring-boot-autoconfigure.jar下就有这个文件

点进去几个注解可发现, 都有这么几个注解:

@ConditionalOnBean:              容器中有指定的Bean时
@ConditionalOnMissingBean: 没有 @ConditionalOnClass: 类路径下游指定的类时
@ConditionalOnMissingClass: 没有 @ConditionalOnExpression: 基于spel表达式判断
@ConditionalOnJava: 基于JVM版本判断
@ConditionalOnJndi: 基于JNDI存在的条件下查找指定位置
@ConditionalOnProperty: 指定的属性是否有指定的值
@ConditionalOnResource: 类路径是否有指定的值
@ConditionalOnSingleCadidate: 指定的bean在容器中只有一个, 或者有多个但是指定首选的bean @ConditionalOnWebApplication: 是Web项目的时候
@ConditionalOnNotWebApplication: 不是Web项目的时候

实例分析:

找到 HttpEncodingAutoConfiguration 并点击进去:

配置类: 
@Configuration
@EnableConfigurationProperties(HttpEncodingProperties.class)    // 开启属性注入声明, @Autowired注入
@ConditionalOnWebApplication
@ConditionalOnClass(CharacterEncodingFilter.class)      // 在类路径下
// 配置文件中有这个配置才会生效
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {
  
  // 注入properties
private final HttpEncodingProperties properties; public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {
this.properties = properties;
}

  // 设置
@Bean
@ConditionalOnMissingBean(CharacterEncodingFilter.class)
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter;
}
}

上面需要一个HttpEncodingProperties, 这儿再看一下

其他属性未列出, 只看需要的:

@ConfigurationProperties(prefix = "spring.http.encoding")
public class HttpEncodingProperties { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
public Charset getCharset() {
return this.charset;
} public void setCharset(Charset charset) {
this.charset = charset;
}
}

其实是一个用来读取yml配置的安全属性注入的类, 没有配置就默认使用UTF8;

实战

新建一个maven项目:

pom.xml

<!-- springboot的自动配置, 自定义starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

属性注入类:

package com.wenbronk.core.autoconfig;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* 自定义starter的配置类
* Created by wenbronk on 2017/5/18.
*/
@ConfigurationProperties(prefix = "hello")
public class HelloProperties { public static final String MSG = "world"; private String msg = MSG; public void setMsg(String msg) {
this.msg = msg;
} public String getMSG() {
return MSG;
}
}

实例类:

package com.wenbronk.core.autoconfig;

/**
* 执行类
* 根据这个类是否存在, 来创建这个类的Bean
* Created by wenbronk on 2017/5/18.
*/
public class HelloService { private String msg; public String sayHello() {
return "hello" + msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }

自动配置类:

package com.wenbronk.core.autoconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
*
* 自动配置类
* Created by wenbronk on 2017/5/18.
*/
@Configuration
@EnableConfigurationProperties(HelloProperties.class) // 声明开启属性注入, 通过 @Autowired注入
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "hello", value = "enabled", matchIfMissing = true)
public class AutoConfigHello { @Autowired
private HelloProperties helloProperties; @Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setMsg(helloProperties.getMSG());
return helloService;
} }

如果想要配置生效, 需要在resources/META-INF/spring.propertes

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wenbronk.core.autoconfig.AutoConfigHello

此时将自己的项目打包, 然后发布在私服上, 就可以在其他项目中通过引入依赖 进行自动开启了

原创地址: http://www.cnblogs.com/wenbronk/p/6873627.html, 转载请注明出处, 谢谢

springboot-22-自定义starter的更多相关文章

  1. (springboot)自定义Starter

    要引入的jar项目,即自定义的Starter项目: pom:(这里不能引入springboot整合否则测试项目注入失败) <?xml version="1.0" encodi ...

  2. SpringBoot编写自定义Starter

    根据SpringBoot的Starter编写规则,需要编写xxxStarter依赖xxxAutoConfigurer,xxxStarter是一个空的jar,仅提供辅助性的依赖管理,引入其他类库 1.建 ...

  3. springboot 简单自定义starter - beetl

    使用idea新建springboot项目beetl-spring-boot-starter 首先添加pom依赖 packaging要设置为jar不能设置为pom<packaging>jar ...

  4. springboot 简单自定义starter - dubbo

    首先需要引入pom 这里使用nacos注册中心 所以引入了nacos-client 使用zookeeper注册中心的话需要引入其相应的client <dependency> <gro ...

  5. SpringBoot应用篇(一):自定义starter

    一.码前必备知识 1.SpringBoot starter机制 SpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进starter,应用者只需要在mave ...

  6. SpringBoot之旅第六篇-启动原理及自定义starter

    一.引言 SpringBoot的一大优势就是Starter,由于SpringBoot有很多开箱即用的Starter依赖,使得我们开发变得简单,我们不需要过多的关注框架的配置. 在日常开发中,我们也会自 ...

  7. java框架之SpringBoot(10)-启动流程及自定义starter

    启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...

  8. SpringBoot第十六篇:自定义starter

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   这一段时间 ...

  9. SpringBoot自定义starter及自动配置

    SpringBoot的核心就是自动配置,而支持自动配置的是一个个starter项目.除了官方已有的starter,用户自己也可以根据规则自定义自己的starter项目. 自定义starter条件 自动 ...

  10. SpringBoot自定义Starter实现

    自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...

随机推荐

  1. R12 更新对应用户的字符集

    R12 更新对应用户的字符集     症状:应用系统数据导出操作,经常发生导出文件(XLS / TSV)产生简体中文乱码. 方案:针对客户端当前用户进行字符集更新 ZHS16GBK,而不影响其他用户. ...

  2. 关于windows服务注册的问题

    开发工具:VS2012 语言:C# 今天的工作内容是把wcf服务以windows服务的方式运行,由于之前没有做过windows服务,所有在网上找了些文章来看下,发现创建windows 服务是一件很简单 ...

  3. SEGGER J-Link install

    Why J-Link? In case you wonder why GNU ARM Eclipse decided to provide support to SEGGER J-Link, the ...

  4. 【转】如何避免OOM总结

    原文地址:http://www.csdn.net/article/2015-09-18/2825737/3 减小对象的内存占用 避免OOM的第一步就是要尽量减少新分配出来的对象占用内存的大小,尽量使用 ...

  5. C# winform截图、web Cropper图片剪切、上传

    今天又来一弹,写了个小功能,windows 桌面截图,web剪切图片上传的功能. 废话不多说,直接上图: 1.winform 截屏功能 图1 主窗体 点击全屏截图,就已经全屏截图了,截图后,图片保存在 ...

  6. ubuntu 16.04.1 nginx彻底删除与重新安装

    1.删除nginx,-purge包括配置文件 sudo apt-get --purge remove nginx 2.移除全部不使用的软件包 sudo apt-get autoremove 3.罗列出 ...

  7. Cesium Language (CZML) 入门1 — CZML Structure(CZML的结构)

    原文:https://github.com/AnalyticalGraphicsInc/cesium/wiki/CZML-Structure CZML是一种用来描述动态场景的JSON架构的语言,主要用 ...

  8. autofac JSON文件配置

    autofac是比较简单易用的IOC容器.下面我们展示如何通过json配置文件,来进行控制反转. 需要用到以下程序集.可以通过nugget分别安装 Microsoft.Extensions.Confi ...

  9. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option)

    今天运行Redis时发生错误,错误信息如下: org.springframework.dao.InvalidDataAccessApiUsageException: MISCONF Redis is ...

  10. RN 47 初始化 Bridge 过程

    - (instancetype)initWithDelegate:(id<RCTBridgeDelegate>)delegate bundleURL:(NSURL *)bundleURL ...