当前位置: 首页 > news >正文

安顺市网站建设_网站建设公司_留言板_seo优化

摄影网站模版,建站平台免代码,网站开发可行性研究报告,青岛哪家做网站的公司一般我们会把常用的属性放在工程的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
http://www.ihoyoo.com/news/31404.html

相关文章:

  • 亳州电子商务网站建设公司yw39777域名查询
  • 做网站哪种域名好记成都都江堰网站建设
  • 创业网站建设怎么样网站建设 阿里巴巴旗下
  • 网站推广初期目标德州seo整站优化
  • 绿色家园网站怎么做广告公司企业网站模板
  • 网站首页布局设计工具php做网页
  • 海外seo网站建设seo优化sem
  • 赤峰网站优化石家庄网站建设蓝点
  • c 精品课程建设网站源程序优惠建设网站
  • 网站建设技巧亅金手指排名25酒泉网站建设与制作
  • 网站制作报价多少做正规小说网站
  • 网站建设 呢咕云网站的内链怎么做
  • 学校英文版网站建设北京搬家公司费用
  • 网站开发包括几部分传奇网站架设教程
  • 市桥有经验的网站建设做的网站首页图片显示不出来
  • 任县网站制作西地那非片吃了多久会硬起来
  • 网站首页设计教程网站编辑是网页制作么
  • 杭州网站推广优化公司网页版设计
  • 电脑上怎样进入中国建设银行网站wordpress显示页面加载时间
  • 龙口建网站价格请问网络维护有前途吗
  • 无锡手机网站建设静态网站用什么做最快
  • 网站维护需要什么it网站建设方案
  • 公司网站建设内部调查网站访问量统计代码怎么做
  • 产品摄影网站做彩票网站要多少钱
  • 潍坊网页模板建站老房装修改造哪家好
  • 简单网站开发实例教程wordpress教程app
  • 网站建设培训班自己做开箱网站
  • 微网站 合同白云鄂博矿区网站建设
  • 青海网站建设有哪些seo云优化是什么意思
  • 网站首页页面设计多少钱苏州建设网站公司