Spring Boot深度课程系列
峰哥说手艺—2020庚子年重磅推出、战胜病毒、我们在行动
06 峰哥说手艺:手撸Spring Boot自定义启动器,解密Spring Boot自动化设置原理
Spring Boot中的启动器是若何启动的,很多人都对这个问题搞不清晰。今天峰哥带着人人
通过自定义一个启动器,可以很容易的看懂Spring Boot是怎么样自动设置的。下面我们通过一个案例来说明这个问题。步骤如下
A)新建一个通俗的Maven项目,要害部门截图如下。
B)在工程的pom.xml文件中添加Spring boot自动化设置依赖,其他的部门无需修改。
org.springframework.boot
spring-boot-autoconfigure
2.2.4.RELEASE
C)设置完成后,我们首先建立一个 HelloProperties 类,用来接受 application.properties 中注入的值,如下
package com.java;
@ConfigurationProperties(prefix = “author”)
public class HelloProperties {
private static String DEFAULT_NAME=”罗贯中”;
private static Integer DEFAULT_AGE=34;
private String name=DEFAULT_NAME;
private Integer age=DEFAULT_AGE;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
该设置类很好明白,我们在后面新建一个工程,可以根据需要,通过application.properties将我们自已设置的属性值注入到name和age中。若是没有注入就会使用默认的值“罗贯中”和34,让属性值为这两个。类上的注解我们前面学过,应该也不生疏。
D)建立一个service包,建立营业类HelloService.功效异常简朴,就是一个输出name和age的功效。编码如下:
package com.java.service;
public class HelloService {
private String name;
private Integer age;
public String sayHello(){
return name+”的岁数是:”+age;
}
//getter和setter省略…
}
E)编写自动设置类HelloServiceAutoConfiguration,先写代码。然后后面举行注释说明。
package com.java.configuration;
import com.java.HelloProperties;
import com.java.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
HelloService helloService(){
HelloService helloService=new HelloService();
helloService.setName(helloProperties.getName());
helloService.setAge(helloProperties.getAge());
return helloService;
}
}
对自动设置类的注解举行注释说明:
l @Configuration解释这是一个设置配。
l @EnableConfigurationProperties(HelloProperties.class)示意自动设置属性类,让HelloProperties类的属性进入Spring的Bean中。
l @ConditionalOnClass(HelloService.class):这个注解是Conditional注解的子注解,意思是表示当项目当前 classpath 下存在若是HelloService类存在,后面的设置才生效,这个原理就是我们前面解说的条件注解,人人可以回忆一下条件注解的用法。
E)建立资源文件夹,在resources文件夹下面建立META-INF文件夹,然后建立spring.factories文件。实在这个我们可以随便参考一个启动器,就能很清晰的知道。
Spring.factories的内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.java.configuration.HelloServiceAutoConfiguration
F)打包到内陆堆栈供我们使用,在第一个工程chapter01中添加如下依赖,并在HelloController中添加测试方式。代码如下:
G)修改application.yml,然后对author举行属性设置。再次举行测试,最后效果如下:
l application.yml
spring:
profiles:
active: dev
—
server:
port: 8083
author:
name: 老大
age: 56
l pom.xml添加依赖
com.java
HelloWorldStarter
1.0-SNAPSHOT
HelloController类添加如下代码
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(“/test”)
public String test(){
return helloService.sayHello();
}
}
在浏览器输入:http://localhost:8083/test
问题:清说明Spring boot自动设置的原理?
Spring Boot启动的时刻会通过@EnableAutoConfiguration注解找到META-INF/spring.factories设置文件中的所有自动设置类,并对其举行加载,而这些自动设置类都是以AutoConfiguration末端来命名的,它实际上就是一个JavaConfig形式的Spring容器设置类,它能通过以Properties末端命名的类中取得在全局设置文件中设置的属性如:server.port,而XxxxProperties类是通过@ConfigurationProperties注解与全局设置文件中对应的属性举行绑定的。
人人可以动手写一下,无需半个小时。峰哥会根据一定的节奏,在民众号公布,同时人人也可以关注我在博客园中的专栏https://www.cnblogs.com/027kgc/.对于文章中的内容,峰哥会陆续最先免费的公开课。
原文链接:https://www.cnblogs.com/027kgc/p/12393825.html
本站声明:网站内容来源于网络,若有侵权,请联系我们,我们将及时处理。
SpringBoot操作redis之String数据结构