如何从属性文件中读取值?
我正在使用弹簧。我需要从属性文件中读取值。这是内部属性文件而不是外部属性文件。属性文件可以如下所示。
some.properties ---file name. values are below.
abc = abc
def = dsd
ghi = weds
jil = sdd
我需要从属性文件中读取这些值,而不是以传统方式。如何实现? spring 3.0有什么最新的方法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在您的上下文中配置 PropertyPlaceholder:
然后引用 bean 中的属性:
使用多个逗号分隔值解析属性:
如果这不起作用,您可以使用属性定义一个 bean,手动注入并处理它:
以及 bean :
Configure PropertyPlaceholder in your context:
Then you refer to the properties in your beans:
To parse property with multiple comma-separated values:
If that doesn't work, you can define a bean with properties, inject and process it manually:
and the bean:
有多种方法可以实现相同的目的。下面是spring中一些常用的方式
使用PropertyPlaceholderConfigurer
使用 PropertySource
使用 ResourceBundleMessageSource
使用 PropertiesFactoryBean
还有更多......................
假设
ds.type
是属性文件中的关键。使用
PropertyPlaceholderConfigurer
注册
PropertyPlaceholderConfigurer
bean-或
或
注册
PropertySourcesPlaceholderConfigurer
后,您可以访问值- 使用
PropertySource
在最新的 Spring 版本中,您不需要使用
@PropertySource
注册PropertyPlaceHolderConfigurer
,我找到了一个很好的链接来了解版本兼容性 -使用
ResourceBundleMessageSource
注册Bean-
访问值-
或
使用
PropertiesFactoryBean
将Bean-
Wire Properties实例注册到您的类中-
There are various ways to achieve the same. Below are some commonly used ways in spring-
Using PropertyPlaceholderConfigurer
Using PropertySource
Using ResourceBundleMessageSource
Using PropertiesFactoryBean
and many more........................
Assuming
ds.type
is key in your property file.Using
PropertyPlaceholderConfigurer
Register
PropertyPlaceholderConfigurer
bean-or
or
After registering
PropertySourcesPlaceholderConfigurer
, you can access the value-Using
PropertySource
In the latest spring version you don't need to register
PropertyPlaceHolderConfigurer
with@PropertySource
, I found a good link to understand version compatibility-Using
ResourceBundleMessageSource
Register Bean-
Access Value-
or
Using
PropertiesFactoryBean
Register Bean-
Wire Properties instance into your class-
在配置类中
In configuration class
这是一个额外的答案,对我理解它的工作原理也有很大帮助: http: //www.javacodegeeks.com/2013/07/spring-bean-and-propertyplaceholderconfigurer.html
Here is an additional answer that was also great help for me to understand how it worked : http://www.javacodegeeks.com/2013/07/spring-bean-and-propertyplaceholderconfigurer.html
如果您需要在不使用@Value的情况下手动读取属性文件。
感谢 Lokesh Gupta 写得很好的页面:博客
If you need to manually read a properties file without using @Value.
Thanks for the well written page by Lokesh Gupta : Blog
另一种方法是使用 ResourceBundle。基本上,您可以使用不带“.properties”的名称来获取捆绑包,
并且可以使用以下命令恢复任何值:
Another way is using a ResourceBundle. Basically you get the bundle using its name without the '.properties'
And you recover any value using this:
您需要将 PropertyPlaceholderConfigurer bean 放入应用程序上下文中并设置其位置属性。
请参阅此处的详细信息:http://www.zparacha.com/ how-to-read-properties-file-in-spring/
您可能需要稍微修改一下属性文件才能使此功能正常工作。
希望有帮助。
You need to put a PropertyPlaceholderConfigurer bean in your application context and set its location property.
See details here : http://www.zparacha.com/how-to-read-properties-file-in-spring/
You might have to modify your property file a bit for this thing to work.
Hope it helps.
我想要一个不由 spring 管理的实用程序类,因此没有像
@Component
、@Configuration
等 Spring 注释。但我希望该类从application 读取.properties
我设法通过让类了解 Spring Context 来使其工作,从而了解
Environment
,从而了解environment.getProperty()
按预期工作。明确地说,我有:
application.properties
Utils.java
ApplicationContextProvider.java (请参阅 Spring 获取当前ApplicationContext)
I wanted an utility class which is not managed by spring, so no spring annotations like
@Component
,@Configuration
etc. But I wanted the class to read fromapplication.properties
I managed to get it working by getting the class to be aware of the Spring Context, hence is aware of
Environment
, and henceenvironment.getProperty()
works as expected.To be explicit, I have:
application.properties
Utils.java
ApplicationContextProvider.java (see Spring get current ApplicationContext)
我建议阅读此链接 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 来自 SpringBoot 文档,了解如何注入外部配置。他们不仅讨论了从属性文件中检索,还讨论了 YAML 甚至 JSON 文件。我发现这很有帮助。我希望你也这样做。
I'll recommend reading this link https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html from SpringBoot docs about injecting external configs. They didn't only talk about retrieving from a properties file but also YAML and even JSON files. I found it helpful. I hope you do too.
在 Spring Boot 版本 3 中;
创建如下文件: GlobalProperties.java
将文件 global.properties 添加到 application.properies 旁边的资源文件夹中,然后粘贴以下内容
现在您可以在每个服务中 @Autowired ,例如这:
In Spring Boot version 3;
Create a file like this : GlobalProperties.java
Add file global.properties to your resources folder beside the application.properies and paste the following
Now you can @Autowired in every service like this: