springboot将yml文件中的值注入到静态属性

硅谷探秘者 2069 0 0
yml配置文件:
sys: 
  qq_back: http://www.123.club/indexes
  qq_appid: 1018
  qq_appkey: 025bf1d0
配置类
public class SystemStaticConstant {

	private static String qq_back;
	
	private static String qq_appid;
	
	private static String qq_appkey;
	
	public static String getQq_back() {
		return qq_back;
	}
	public static String getQq_appid() {
		return qq_appid;
	}
	public static String getQq_appkey() {
		return qq_appkey;
	}
}
自动注入适配器

项目启动中执行,完成注入。

import java.io.File;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

import com.baidu.aip.util.PhotoCheckUpService;

@Component
@Order(value=1)
public class SystemAttributeConfig implements CommandLineRunner {
	
	@Autowired
	private ConfigurableEnvironment environment;
	
    private static Map<String, String> attributeMap = new ConcurrentHashMap<>();

    /**
     * 指定需要自动注入的范围
     */
    private static final ConfigurationPropertyName attributes = ConfigurationPropertyName.of("sys");
    private static final Bindable<Map<String, String>> STRING_STRING_MAP = Bindable.mapOf(String.class, String.class);
	

    @Override
    public void run(String... var1) throws Exception {
    	/**
    	 * 	在SystemStaticConstant类中配置常量
    	 */
    	Binder binder = Binder.get(environment);
    	attributeMap = binder.bind(attributes, STRING_STRING_MAP).orElseGet(Collections::emptyMap);
        System.out.println(attributeMap);
        
    	Class<SystemStaticConstant> c = SystemStaticConstant.class;
    	Field[] fs = c.getDeclaredFields();
    	for(Field f : fs) {
			try {
				f.setAccessible(true);
				if(attributeMap.containsKey(f.getName())) {
					if(f.getType()==String.class) {
						f.set(c,attributeMap.get(f.getName()));
					}else if(f.getType()==Integer.class||f.getType()==int.class) {
						f.set(c,new Integer(attributeMap.get(f.getName())));
					}else if(f.getType()==Long.class||f.getType()==long.class) {
						f.set(c,new Long(attributeMap.get(f.getName())));
					}else if(f.getType()==Double.class||f.getType()==double.class) {
						f.set(c,new Double(attributeMap.get(f.getName())));
					}else if(f.getType()==Short.class||f.getType()==short.class) {
						f.set(c,new Short(attributeMap.get(f.getName())));
					}else if(f.getType()==Float.class||f.getType()==float.class) {
						f.set(c,new Float(attributeMap.get(f.getName())));
					}else if(f.getType()==Boolean.class||f.getType()==boolean.class) {
						f.set(c,new Boolean(attributeMap.get(f.getName())));
					}
				}
			} catch (Exception e) {
				throw new Exception(e.getMessage()+"----配置文件类型转换异常--->"+f.getName());
			}
    	}
    }
}

 


评论区
请写下您的评论...
暂无评论...
猜你喜欢
spring/springmvc 2396 创建一个MarkControllerMarkController类上有@RestController
框架 2226 maven打包动替换配置pom添加profilesprofileiddev/idpropertiesprofileActivedev/profileActive
java基础 4447 时候并没有创建Main4实例对象,而这时再方法new内部类对象就无法访问外部类(因为外部类并没有对象),所以就会发送冲突。
框架 7819 多么痛领悟~分离资源打包后运行项目,启动失败数据源初始化失败~检查问题,这种情况下没有打印错误日志,首先配置一下日志,错误报告在控制台打印出来。resources夹下创建一个
weblog 3152 usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespaceConsoleApplication3 { classProgram { staticvoidMain(string[]args) { Students=n
框架 3800 SpringBoot打包分离资源springboot项目打包时,把资源,如配置资源等分离出来,避免为了修改资源时重新打包。方式11.pombuildplugins
weblog 2198 vue计算监视!DOCTYPEhtmlhtml head metacharset="UTF-8" title/title scriptsrc="js/vue.min.js
linux系统 1734 查看配置centos7网络IP地址配置在/etc/sysconfig/network-scripts/ifcfg-ens33下输命令ifconfig查看当前ip地址修改配置输命令:vim
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。