本文共 7368 字,大约阅读时间需要 24 分钟。
带你玩转属于自己自己的spring-boot-starter系列(一)如何将自己编写的一个很牛叉的鉴权模块集成到当前的系统中。那么我们总是有些参数是需要进行配置的,不可能我们的starter里面没有参数需要配置,本章将教大家如何实现将配置信息导入到我们的spring体系中,比如我们若不在application.properties中设置我们的server.port的端口,为什么他的端口默认就是8080,若你有类似的需求场景,那么请接着往下看即可。
首先我们使用idea创建一个鉴权模块名称为“spring-starter-config-core”,以下为创建完成以后的pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE com.lazyboyl.config spring-starter-config-core 0.0.1-SNAPSHOT spring-starter-config-core 这是一个实现配置注入的过程 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.apache.maven.plugins maven-compiler-plugin 3.6.1
工程目录如下所示:
接着我们在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; }}
接着我们需要在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)); }}
最后我们创建我们的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 { }
最后在我们的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" } ]}
首先我们使用idea创建一个鉴权模块名称为“spring-starter-config-demo-one”,这边一定要记得引入前面我们编写好的核心工程的maven依赖,以下为创建完成以后的pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.4.RELEASE com.lazyboyl.config spring-starter-config-demo-one 0.0.1-SNAPSHOT spring-starter-config-demo-one 验证配置文件是否生效的例子 1.8 com.lazyboyl.config spring-starter-config-core 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
代码如下:
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 * 类描述: */@RestControllerpublic 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"; }}
我们直接启动当前的“spring-starter-config-demo-one”这个工程,接着我们浏览器地址栏输入http:\127.0.0.1:8080/test,这时候大家可以在控制台看到如下的信息:
到此处就说明我们的默认配置生效了,若我们不做任何的设置则使用我们之前配置好的默认配置,接着我们在我们的“spring-starter-config-demo-one”的application.properties中修改以下的配置:xxxface.key=sadsadaxxxface.url=http://ww.baidu.com
然后接着重启我们的“spring-starter-config-demo-one”这个工程,接着我们浏览器地址栏输入http:\127.0.0.1:8080/test,这时候大家可以在控制台看到如下的信息:
到此为止我们就验证了我们的设想了。转载地址:http://gakvf.baihongyu.com/