带你玩转属于自己自己的spring-boot-starter系列(一)如何将自己编写的一个很牛叉的鉴权模块集成到当前的系统中。那么我们总是有些参数是需要进行配置的,不可能我们的starter里面没有参数需要配置,本章将教大家如何实现将配置信息导入到我们的spring体系中,比如我们若不在application.properties中设置我们的server.port的端口,为什么他的端口默认就是8080,若你有类似的需求场景,那么请接着往下看即可。
1、编写配置的核心模块
1.1、创建配置模块的核心工程
首先我们使用idea创建一个鉴权模块名称为“spring-starter-config-core”,以下为创建完成以后的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lazyboyl.config</groupId>
<artifactId>spring-starter-config-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-starter-config-core</name>
<description>这是一个实现配置注入的过程</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
工程目录如下所示:
1.2、创建属性的枚举类
接着我们在constant包底下创建配置的枚举类,代码如下所示:
package com.lazyboyl.config.core.constant;
/**
* @author linzef
* 类描述: 配置属性的枚举类
*/
public enum FaceConstant {
URL("xxxface.url","http://xxx.face.com/get"),
KEY("xxxface.key","abcdefghijklmn");
private String key;
private String defaultValue;
FaceConstant(String key, String defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}
public String getKey() {
return key;
}
public String getDefaultValue() {
return defaultValue;
}
}
1.3、实现spring的注入
接着我们需要在spring的生命周期中注入我们的相关属性,实现将我们的枚举类里的属性注入到我们的spring体系中,代码如下所示:
package com.lazyboyl.config.core.config;
import com.lazyboyl.config.core.constant.FaceConstant;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.type.AnnotationMetadata;
import java.util.Properties;
/**
* @author linzf
* @since 2020/9/21
* 类描述: 将默认的注解注入spring体系的
*/
public class FaceScannerRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
ConfigurableEnvironment c = (ConfigurableEnvironment) environment;
MutablePropertySources m = c.getPropertySources();
Properties p = new Properties();
for (FaceConstant fc : FaceConstant.values()) {
String val = environment.getProperty(fc.getKey());
if (val == null || "".equals(val)) {
p.put(fc.getKey(), fc.getDefaultValue());
}
}
m.addFirst(new PropertiesPropertySource("defaultProperties", p));
}
}
1.4、创建我们的starter的配置文件
最后我们创建我们的starter配置文件,实现将我们的上面配置的属性注入到相应的工程中,代码如下:
package com.lazyboyl.config.core.autoconfigure;
import com.lazyboyl.config.core.config.FaceScannerRegister;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author linzf
* @since 2020/9/21
* 类描述:
*/
@Configuration
@Import({FaceScannerRegister.class})
public class StarterAutoConfigure {
}
1.5、配置我们的starter
最后在我们的resource文件夹底下创建一个META-INF文件夹,然后再这个文件夹底下创建一个spring.factories文件,该文件的内容如下所示:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lazyboyl.config.core.autoconfigure.StarterAutoConfigure
接着我们新增一个spring-configuration-metadata.json配置文件,主要用于在我们使用IDEA开发的时候当我们输入xxxface的时候回给出相应的提示,代码如下所示:
{
"properties": [
{
"name": "xxxface.url",
"type": "java.lang.String",
"description": "脸部识别的地址",
"defaultValue": "http://xxx.face.com/get"
},
{
"name": "xxxface.key",
"type": "java.lang.String",
"description": "脸部识别的key",
"defaultValue": "abcdefghijklmn"
}
]
}
2、验证我们的配置工程
2.1、创建application不做任何配置的时候是否可以正常读取我们的配置文件
首先我们使用idea创建一个鉴权模块名称为“spring-starter-config-demo-one”,这边一定要记得引入前面我们编写好的核心工程的maven依赖,以下为创建完成以后的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lazyboyl.config</groupId>
<artifactId>spring-starter-config-demo-one</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-starter-config-demo-one</name>
<description>验证配置文件是否生效的例子</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.lazyboyl.config</groupId>
<artifactId>spring-starter-config-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2、创建验证的代码
代码如下:
package com.lazyboyl.config.demo.one.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author linzf
* @since 2020/9/21
* 类描述:
*/
@RestController
public class ConfigController {
@Value("${xxxface.url}")
private String url;
@Value("${xxxface.key}")
private String key;
@GetMapping("test")
public String test(){
System.out.println(url + "=====" + key);
return "test";
}
}
2.3、验证效果
我们直接启动当前的“spring-starter-config-demo-one”这个工程,接着我们浏览器地址栏输入http:\127.0.0.1:8080/test,这时候大家可以在控制台看到如下的信息:
到此处就说明我们的默认配置生效了,若我们不做任何的设置则使用我们之前配置好的默认配置,接着我们在我们的“spring-starter-config-demo-one”的application.properties中修改以下的配置:
xxxface.key=sadsada
xxxface.url=http://ww.baidu.com
然后接着重启我们的“spring-starter-config-demo-one”这个工程,接着我们浏览器地址栏输入http:\127.0.0.1:8080/test,这时候大家可以在控制台看到如下的信息:
到此为止我们就验证了我们的设想了。