SpringBoot自动配置实现
2017-09-24 15:49
776 查看
创建SpringBoot自动配置
新建一个Maven工程创建安全配置类
package com.cvsea.test.spring_boot_starter_hello; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix="hello")//引用application.properties中的配置 public class HelloServiceProperties { private static final String MSG="world"; private String msg=MSG; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
创建使用配置的Service
package com.cvsea.test.spring_boot_starter_hello; public class HelloService { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String sayHello() { return "Hello,"+msg; } }
创建自动配置类
package com.cvsea.test.spring_boot_starter_hello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 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; @Configuration @EnableConfigurationProperties(HelloServiceProperties.class) @ConditionalOnClass(HelloService.class) @ConditionalOnProperty(prefix="hello",value="enabled",matchIfMissing=true) public class HelloServiceAutoConfiguration { @Autowired private HelloServiceProperties helloServiceProperties; @Bean public HelloService helloService() { HelloService helloService=new HelloService(); helloService.setMsg(helloServiceProperties.getMsg()); return helloService; } }
使用spring.factories注册自动配置类
org.springframework.boot.autoconfiguration.EnableAutoConfig=com.cvsea.test.spring_boot_starter_hello.HelloServiceAutoConfiguration
测试
:使用starter创建一个SpringBoot项目,加入刚刚实现的自动配置的starter作为依赖<dependency> <groupId>com.cvsea</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
在application.properties添加配置内容
hello.msg=pxs
调用自动配置中的方法
package com.cvsea; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.cvsea.test.spring_boot_starter_hello.HelloService; @RestController @SpringBootApplication public class Learning1Application { @Autowired HelloService helloService; @RequestMapping("/") public String hello() { return helloService.sayHello(); } public static void main(String[] args) { SpringApplication.run(Learning1Application.class, args); } }
相关文章推荐
- spring-boot 是如何实现自动配置的
- springboot源码解析:自己实现一个springboot自动配置
- 面试问题spring boot自动配置是怎么实现的
- SpringBoot自动配置的实现原理
- [Spring Boot] 3. Spring Boot实现自动配置的基础
- Spring Boot 自动配置理解 以及实现自定义Starter
- 【springboot】基于springboot运行原理实现springboot的自动配置
- [Spring Boot] 4. Spring Boot实现自动配置的原理
- Spring Boot实现自动配置的基础
- SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置
- Spring Boot 揭秘与实战 自己实现一个简单的自动配置模块
- 全面解析SpringBoot自动配置的实现原理
- Spring Boot 自动配置的实现
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- SpringBoot+ZooKeeper+ZKUI+Drools 实现应用配置中心及业务规则动态加载
- 学记:为spring boot写一个自动配置
- Spring Boot自动配置详解
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
- 【spring boot】配置ssl证书实现https