博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot-写Starter
阅读量:2350 次
发布时间:2019-05-10

本文共 2068 字,大约阅读时间需要 6 分钟。

test-spring-boot-autoconfigure

在这里插入图片描述
pom引入

org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
org.springframework.boot
spring-boot-starter
org.projectlombok
lombok

配置文件类

@ConfigurationProperties(prefix = "test.hello")public class HelloProperties {
private String suffix; public String getSuffix() {
return suffix; } public void setSuffix(String suffix) {
this.suffix = suffix; }}

服务类

public class HelloService {
HelloProperties helloProperties; public String sayHello(String name) {
return "Hello " + name + "," + helloProperties.getSuffix(); } public HelloProperties getHelloProperties() {
return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties; }}

注册类

/** * web应用才生效 */@ConditionalOnWebApplication/** * 让属性文件生效 */@EnableConfigurationProperties(HelloProperties.class)/*** * 声明是一个配置类 */@Slf4j@Configurationpublic class HelloServiceAutoConfiguration {
@Autowired private HelloProperties helloProperties; @Bean public HelloService helloService() {
log.info("开始注册HelloService Bean"); HelloService helloService = new HelloService(); helloService.setHelloProperties(helloProperties); return helloService; }}

test-spring-boot-starter

在这里插入图片描述
为空,pom.xml引入

com.test
test-spring-boot-autoconfigure
1.0-SNAPSHOT

配置类

测试在SpringBoot项目中,pom.xml引入自动配置项目

com.test
test-spring-boot-starter
1.0-SNAPSHOT

配置

test.hello.suffix=早上好

注入HelloService,即可使用。

@Autowired    private HelloService helloService;

参考 https://www.cnblogs.com/niumoo/p/11775009.html

你可能感兴趣的文章
em单位的理解和使用
查看>>
localStorage的理解和应用
查看>>
base64图片编码大小与原图文件大小之间的联系
查看>>
安装和认识express框架
查看>>
三种主流的JVM(JDK)使用心得
查看>>
多核危机:Scala vs. Erlang
查看>>
未来系统中的编程语言
查看>>
函数式编程另类指南1
查看>>
kudu tablet design(kudu表设计)
查看>>
kudu master design(kudu主节点设计)
查看>>
第七章:druid.io实践分享之realtime+kafka 一
查看>>
第七章:druid.io实践分享之Realtime+kafka 二
查看>>
Java内存管理
查看>>
Maven配置资料(一)
查看>>
Maven配置资料(二)
查看>>
hive脚本运行查看错误日志方式
查看>>
hive导入数据丢失问题
查看>>
linux查看网卡驱动
查看>>
hadoop运行任务时出现网络链接异常
查看>>
设置MAC OS远程登陆
查看>>