朋友做的网站图片不显示不出来,中文域名价格,网站编程薪资,小程序开发平台官网入口一般我们会把常用的属性放在工程的classpath文件夹中#xff0c;以property#xff0c;yaml或json的格式进行文件存储#xff0c;便于Spring-boot在初始化时获取。 Value则是Spring一个非常有用的注解#xff0c;可以在初始化时很方便的对Bean的入参变量进行赋值#xff… 一般我们会把常用的属性放在工程的classpath文件夹中以propertyyaml或json的格式进行文件存储便于Spring-boot在初始化时获取。 Value则是Spring一个非常有用的注解可以在初始化时很方便的对Bean的入参变量进行赋值例如 Beanpublic BusinessClient businessClient (Value(http://baseUrl/) String baseUrl) {Retrofit retrofit new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(BusinessClient .class) 于是初始化好的Business Client进行http请求时默认的baseurl都是”http://baseUrl“。实际上Value还支持一种特殊的写法:”${some.proptery.key}”,即将property的key值写在花括号中。例如我有一个property为aerexu.basurlhttp://baseUrl2将上例中的Value改写成Value(${aerexu.basurl})baseUrl实际获取到的值是http://baseUrl2。 这个特性是利用Spring的bean PropertySourcesPlaceholder实现的Spring boot已经在初始化时帮我们自动实例化了该bean。若是传统的Spring工程则需要主动实例化如下 Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {PropertySourcesPlaceholderConfigurer configurer new PropertySourcesPlaceholderConfigurer(); configurer.setPlaceholderPrefix(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX); configurer.setPlaceholderSuffix(PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX); configurer.setValueSeparator(PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR); return configurer; } 一般情况下property存在工程中的文件就可以了但带来的坏处是如果属性需要改变必须重新发布工程。比如对接上例中的url可能会变为https可能端口会变化。所以这种类型的属性放在数据库中更合适。然而将属性存储在数据库中后Value对应的值就无法正常解析了。因此这里提供一种hack的方法使得Value可以正常解析。PropertySourcesPlaceholder在解析属性时都是从ConfigurableEnvironment中进行寻找的。当ConfigurableEnvironment没有存在的属性时${}写法的Value就无法解析了。因此需要通过特殊的处理将存储在数据库中的属性注入到ConfigurableEnvironment中。本文定义了一个LoadFromDatabasePropertyConfig类实现该功能其代码如下 ConfigurationSlf4jpublic class LoadFromDatabasePropertyConfig { Autowired private ConfigurableEnvironment env; Autowired private SysPropertyResourceMapper propertyResourceMapper; PostConstruct public void initializeDatabasePropertySourceUsage() { MutablePropertySources propertySources env.getPropertySources(); try { MapString, Object propertyMap propertyResourceMapper.selectAll().stream() .collect(Collectors.toMap(SysPropertyResource::getPropertyName, SysPropertyResource::getPropertyValue)); Properties properties new Properties(); properties.putAll(propertyMap); PropertiesPropertySource dbPropertySource new PropertiesPropertySource(dbPropertySource, properties); Pattern p Pattern.compile(^applicationConfig.*); String name null; boolean flag false; for (PropertySource? source : propertySources) { if (p.matcher(source.getName()).matches()) { name source.getName(); flag true; log.info(Find propertySources .concat(name)); break; } } log.info(); if(flag) { propertySources.addBefore(name, dbPropertySource); } else { propertySources.addFirst(dbPropertySource); } } catch (Exception e) { log.error(Error during database properties setup, e); throw new RuntimeException(e); } } } 上述代码的具体思路是将数据库中的所有需要的属性读出通过Properties类转换为Spring可用的PropertiesPropertySource并取名为dbPropertySource。随后利用正则匹配从已有的所有属性中找到名称以applicationConfig开头的属性该属性即是所有配置在文件中的property所解析成的对象并将dbPropertySource存储在其之前。这样当文件和数据库中同时存在key相等的属性时会优先使用数据库中存储的value。需要注意的是上述方案提供的属性解析必须在数据库相关的bean都实例化完成后才可进行。且为了保证bean在实例化时数据库属性已经被加入到ConfigurableEnvironment中去了必须添加DependsOn注解。上面的BusinessClient的实例化就需更新成 BeanDependsOn(loadFromDatabasePropertyConfig)public BusinessClient businessClient (Value(${aerexu.basurl}) String baseUrl) { Retrofit retrofit new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); return retrofit.create(BusinessClient .class) 转载于:https://www.cnblogs.com/bevis-byf/p/11491475.html