这篇文章主要围绕PropertyPlaceholderConfigurer查找数据库值并将属性文件用作后备和查找数据库对象展开,旨在为您提供一份详细的参考资料。我们将全面介绍PropertyPlace
这篇文章主要围绕PropertyPlaceholderConfigurer查找数据库值并将属性文件用作后备和查找数据库对象展开,旨在为您提供一份详细的参考资料。我们将全面介绍PropertyPlaceholderConfigurer查找数据库值并将属性文件用作后备的优缺点,解答查找数据库对象的相关问题,同时也会为您带来.properties文件中的PropertyPlaceholderConfigurer和环境变量、env.getProperty不起作用Spring PropertyPlaceholderConfigurer、java – PropertyPlaceholderConfigurer PropertiesFactoryBean仅解析位置属性、java-Spring:PropertyPlaceholderConfigurer找不到属性文件的实用方法。
本文目录一览:- PropertyPlaceholderConfigurer查找数据库值并将属性文件用作后备(查找数据库对象)
- .properties文件中的PropertyPlaceholderConfigurer和环境变量
- env.getProperty不起作用Spring PropertyPlaceholderConfigurer
- java – PropertyPlaceholderConfigurer PropertiesFactoryBean仅解析位置属性
- java-Spring:PropertyPlaceholderConfigurer找不到属性文件
PropertyPlaceholderConfigurer查找数据库值并将属性文件用作后备(查找数据库对象)
我想知道是否有可能在PropertyPlaceholderConfigurer中使用属性文件之前的DB中的值。因此,我要实现的是加载属性文件,并且如果数据库中存在任何键,请使用该文件。现在我不知道从哪里开始,但是我假设有一个可以重写的方法/类或需要实现的接口。
只需提及方法/类/接口,我将很高兴从这里开始。TIA
答案1
小编典典- PropertyPlaceholderConfigurer具有一个“属性”属性,该属性可以指向检索数据库值的对象。在此处查看示例:[http](http://pure-
- essence.net/2011/02/10/spring-loading-properties-from-database-with-a-twist/)
- //pure-essence.net/2011/02/10/spring-loading-properties-from-database-with-
a-twist/
另外,您将需要在从数据库加载属性的Bean上将“
ignoreUnresolvablePlaceholders”设置为true。这样,您可以添加另一个PropertyPlaceholderConfigurer作为后备,以提供数据库中找不到的属性。
.properties文件中的PropertyPlaceholderConfigurer和环境变量
我有一个带有PropertyPlaceholderConfigurer的Spring application-
context.xml,可以从.properties文件中获取属性的值。主和测试源文件夹具有单独的.properties文件。问题是我需要在.properties文件中使用环境变量。但是当我通过以下方式进行操作时:
property.name=${env.SYSTEM_PROPERTY}
我收到以下错误:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name ''beanName'' defined in class path resource [com/example/applicationContext.xml]: Could not resolve placeholder ''env.SYSTEM_PROPERTY''
而占位符配置器定义为
<bean> <property name="location" value="classpath:com/example/application.properties"/></bean>
关于如何使property.name解释为环境变量(而不是占位符)的任何想法?
最好的问候,德米特里。
答案1
小编典典我可能会完全更改解决方案:直接注入系统属性,而不是注入引用系统属性的属性
例如
@Value("#{ systemProperties[''JAVA_MY_ENV''] }") private String myVar;
要么
<property name ="myVar" value="#{systemProperties[''JAVA_MY_ENV'']}"/>
我使用这样的属性占位符配置器
<bean id="propertyConfigurer"> <property name="locations"> <list> <value>classpath:someprops.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true" /> <property name="searchSystemEnvironment" value="true" /> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
您还必须记住使用以下命令将参数传递给程序
-DJAVA_MY_ENV=xyz
这样,当您运行生产版本时,您可以通过一件事,而在运行测试时,您可以通过另一件事。
同样,我经常做的事情是这样的:
<property name="locations"> <list> <value>classpath:someprops.properties</value> <value>classpath:someprops-{environment}.properties</value> </list> </property>
环境是prod / stage / test / int / ci /
local(每个环境1个-您现在可能只有2个或3个)。您可以将环境变量传递给程序。无论其在本地PC
/测试上的生产/运行情况如何,任何应相同的属性都应位于someprops.properties属性文件中。特定于环境/运行方式的任何内容都将放在更特定的文件中(除非覆盖机制,否则应将其放置在someprops.properties文件以及默认文件中)
例如在classpath:someprops.properties中
url=www.mysite.com
在classpath:someprops-local.properties中
url=localhost
通过使用此基本思想,您可以以干净的方式将测试和程序的正常运行属性分开。
env.getProperty不起作用Spring PropertyPlaceholderConfigurer
我正在使用spring加载属性文件
<bean id="appProperties">
<property name="locations" value="classpath:/sample.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
当我使用时获得财产价值
@Value("${testkey}")
它的工作正常。
但是当我尝试使用env时
@Resource
private Environment environment;
environment.getProperty("testkey") // returning null
java – PropertyPlaceholderConfigurer PropertiesFactoryBean仅解析位置属性
<bean id="myProperties"https://www.jb51.cc/tag/fig/" target="_blank">fig.Propertiesfactorybean"> <property name="locations"> <list> <!-- Order matters,last one to create a property wins! --> <value>classpath:default.properties</value> <value>file:${MYAPP_PROPERTIES_LOCATION:badurl}/application.properties</value> <value>file:${user.home}/developer.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true"/> <bean id="propertyConfigurer"https://www.jb51.cc/tag/fig/" target="_blank">fig.PropertiesPlaceholderConfigurer"> <property name="properties" ref="myProperties"/> <property name="systemPropertiesModeName" value="SYstem_PROPERTIES_MODE_OVERRIDE"/> <property name="searchSystemEnvironment" value="true"/> </bean>
然后在default.properties文件中,我有以下内容(这些是测试属性):
property1=prop1val property2=${property1}
有效:propertyConfigurer正确解析环境变量MYAPP_PROPERTIES_LOCATION和系统变量user.home.但是,最终的属性对象是由工厂bean创建的,结果属性是[property1 = prop1val,property2 = ${property1}].
使用此配置,如何获取myProperties bean中的属性来解析其占位符?我做了大量的研究,包括追踪弹簧代码 – 我可以看到如何以及为什么没有这样做.我希望有一些我不知道的设置!这是我的第一篇文章,所以对我来说很容易:)
解决方法
如下所述,是你的财产档案,
property1=prop1val property2=${property1}
请记住,它是一个带有键值对的漂亮文本文件,它不能接受变量.在这里,如果您打算将property1的值动态复制到property2,则不会发生这种情况.这不是我们应该使用属性文件的方式.
属性文件应该是具有键值对的简单文本文件.因此,保持密钥是原子的,以便您可以从应用程序或applicationcontext.xml文件中逻辑地构造所需的数据.
java-Spring:PropertyPlaceholderConfigurer找不到属性文件
我对Spring使用PropertyPlaceholderConfigurer有一个奇怪的问题.我的一颗豆的设计如下:
<bean name="propertyPlaceholder"https://www.jb51.cc/tag/fig/" target="_blank">fig.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
问题是spring永远找不到jdbc.properties(FileNotFoundException).该文件位于捆绑包类路径中的名为“ resources”的文件夹中(我在Osgi项目中工作).
我尝试了几乎所有组合(“ jdbc.properties”,“ / jdbc.properties”,“ classpath:jdbc.properties”,“ classpath:/jdbc.properties”,“ / resources / jdbc.properties”等). ),但它永远无法正常工作.
对于信息,如果有的话,我会做一些类似的事情:
URL u = someClassLoader.getResource("jdbc.properties");
它确实可以正常工作并找到文件.实际上,我完全无法理解spring的错误.
如果您有任何帮助的想法,请先感谢.我在春天不太有经验,所以我可能在某个地方犯了一个错误.
[编辑]
实际上,这是类加载器的问题:
如果我做 :
new ClassPathResource("jdbc.properties");
它不起作用.但是:
new ClassPathResource("jdbc.properties",someClassIntheBundle.class.getClassLoader());
完美地工作.
我确实相信Spring使用自己的软件包所消耗的ClassLoader.您知道解决这个棘手问题的方法吗?
谢谢,
我们今天的关于PropertyPlaceholderConfigurer查找数据库值并将属性文件用作后备和查找数据库对象的分享已经告一段落,感谢您的关注,如果您想了解更多关于.properties文件中的PropertyPlaceholderConfigurer和环境变量、env.getProperty不起作用Spring PropertyPlaceholderConfigurer、java – PropertyPlaceholderConfigurer PropertiesFactoryBean仅解析位置属性、java-Spring:PropertyPlaceholderConfigurer找不到属性文件的相关信息,请在本站查询。
本文标签: