Java 如何在我的类中的 spring bean 类中获取属性值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19970605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the property value inside a spring bean class in my class ?
提问by antony.ouseph.k
The things I did are:
我做的事情是:
- Created a property file (data.properties).
- Created a Spring.xml (I use property placeholder)
- Created a bean class.
- I have a class where I have to use the url value.
- I have a web.xml which has context-param where i set the param value to the path of the Spring.xml file.
- My codes are given below:
- 创建了一个属性文件 (data.properties)。
- 创建了一个 Spring.xml(我使用属性占位符)
- 创建了一个 bean 类。
- 我有一个必须使用 url 值的类。
- 我有一个具有上下文参数的 web.xml,我将参数值设置为 Spring.xml 文件的路径。
- 我的代码如下:
Propertyfile:url=sampleurl
Spring.xml:
属性文件:url=sampleurl
弹簧.xml:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath*:data.properties*"/>
</bean>
<bean id="dataSource" class="org.tempuri.DataBeanClass">
<property name="url" value="${url}"></property>
</bean>
beanclass
豆类
public class DataBeanClass extends PropertyPlaceholderConfigurer{
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
entry in web.xml
web.xml 中的条目
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:Spring*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Now my problem is I dont know what method of PropertyPlaceholderConfigurer should I override and what should I do to set the value of variable url such that I can call it from other classes using getproperty() method.
现在我的问题是我不知道我应该覆盖 PropertyPlaceholderConfigurer 的什么方法,我应该怎么做来设置变量 url 的值,以便我可以使用 getproperty() 方法从其他类调用它。
采纳答案by Subin Sebastian
You can annotate your bean property like this, then spring will auto inject the property from your property file.
您可以像这样注释您的 bean 属性,然后 spring 将从您的属性文件中自动注入该属性。
@Value("${url}")
private String url;
You dont have to extend PropertyPlaceholderConfigurer
您不必扩展 PropertyPlaceholderConfigurer
Defining your bean like this will auto populate url as well, but annotation seems to be easiest way
像这样定义你的 bean 也会自动填充 url,但注释似乎是最简单的方法
<bean id="dataSource" class="org.tempuri.DataBeanClass">
<property name="url" value="${url}"></property>
</bean>
回答by Sandhu Santhakumar
Properties files can be made accessible to Spring via the following namespace element in Spring.xml
通过 Spring.xml 中的以下命名空间元素,Spring 可以访问属性文件
<context:property-placeholder location="classpath:data.properties" />
And then you can use like
然后你可以使用像
@Autowired
private Environment env;
...
String url=env.getProperty("url"));
Note:
笔记:
using <property-placeholder>
will not expose the properties to the Spring Environment – this means that retrieving the value like this will not work – it will return null
using<property-placeholder>
不会将属性暴露给 Spring Environment——这意味着像这样检索值将不起作用——它将返回 null
回答by shrey
Please do the following process to get property file value in class.
请执行以下过程以获取类中的属性文件值。
Define the bean for property file in your spring.xml
<util:properties id="dataProperties" location="classpath:/data.properties"/>
keep your data.properties under src/main/resources.
Use the following code to get value from property file for e.g to get value of url key in data.properties
在 spring.xml 中定义属性文件的 bean
<util:properties id="dataProperties" location="classpath:/data.properties"/>
将您的 data.properties 放在 src/main/resources 下。
使用以下代码从属性文件中获取值,例如获取 data.properties 中 url 键的值
private @Value("#{dataProperties['url']})
String url;
private @Value("#{dataProperties['url']})
String url;