SpringBoot自动配置实现
来源:互联网 发布:采购系统sql 编辑:程序博客网 时间:2024/05/09 22:11
创建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@SpringBootApplicationpublic class Learning1Application { @Autowired HelloService helloService; @RequestMapping("/") public String hello() { return helloService.sayHello(); } public static void main(String[] args) { SpringApplication.run(Learning1Application.class, args); }}
阅读全文
0 0
- SpringBoot自动配置实现
- SpringBoot自动配置的实现原理
- SpringBoot自动配置的实现原理
- SpringBoot自动配置原理
- springboot webmvc 自动配置
- AxonFramework,SpringBoot自动配置
- SpringBoot自动配置
- SpringBoot自动配置原理
- SpringBoot 自动配置
- springboot学习----自动配置
- SpringBoot 自动配置原理
- SpringBoot--AxonFramework自动配置
- SpringBoot-自动配置1
- SpringBoot-自动配置源码解析
- springboot 禁用mongoDB自动配置
- SpringBoot学习笔记(5) Spring Boot集成Redis实现自动配置
- SpringBoot学习笔记(三) SpringBoot 自动配置原理
- SpringBoot学习笔记(四) SpringBoot Web相关的自动配置
- 控件EditText的setOnEditorActionListener方法的理解
- java 面试题01
- springMvc执行流程和配置文件作用
- 单页Web应用-概念
- 性价比最高的平板电脑品牌旗舰店推荐
- SpringBoot自动配置实现
- 【OpenJ 2469】电池的寿命
- request详解
- JAVA多线程之线程间的通信方式
- pycharm连接mysql数据库
- 重载,重写,重定义
- kaptcha验证码插件的使用
- SQL查询结果为空补充默认值ISNULL
- Java中的volatile