如何从JavaBean(域类)的属性文件中读取值? [英] How to read values from property file in a JavaBean (domain class)?
问题描述
我有一个域类,我想从属性文件中读取值(自动装配messageSource在这里不起作用),所以有什么想法吗? 我正在使用春天,冬眠 这是一个示例:
I have a domain class and I want to read values from property file (autowiring messageSource wouldn't work here) so any ideas ? I am using spring,hibernate and here's a sample:
package com.myapp.domain;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "domain")
public class MyDomain implements Serializable {
private long entityId;
private String domain="some_hardcoded_value" // need to read it from a property file;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
public long getEntityId() {
return entityId;
}
public void setEntityId(long entityId) {
this.entityId = entityId;
}
public void setDomain(String domain) {
this.domain = domain;
}
@Column(name = "domain")
public String getDomain() {
return domain;
}
}
推荐答案
我仍然不明白这个问题,但是我假设您想从属性文件中设置bean属性.
I still don't understand the question, but I'll assume that you want to set bean properties from a properties file.
其他答案显示了如何从.properties文件中获取Properties对象(我将在下面显示其他方式),我将向您展示如何使用Spring的BeanWrapper
接口从其连接属性:
The other answers have shown how to get a Properties object from a .properties file (I'll show additional ways below), I will show you how to wire properties from it using Spring's BeanWrapper
interface:
public static void wireBeanFromProperties(Object bean, Properties props){
BeanWrapper wrapper = new BeanWrapperImpl(bean);
for(Entry<Object, Object> entry:props.entrySet()){
String propertyName = entry.getKey().toString();
if(wrapper.isWritableProperty(propertyName)){
wrapper.setPropertyValue(propertyName, entry.getValue());
}
}
}
或者,如果您确定可以将属性文件中的所有属性都映射到该类的bean属性,则:
Or, if you know for sure that all properties from the properties file can be mapped to this bean properties of the class:
public static void wireBeanFromProperties(final Object bean,
final Properties props){
final BeanWrapper wrapper = new BeanWrapperImpl(bean);
// will throw an exception if the Properties object
// contains any unknown keys
wrapper.setPropertyValues(props);
}
Reference: 5.4. Bean manipulation and the BeanWrapper
Actually, the Spring-specific ways to load resources from the classpath use the Resource
mechanism
InputStream str = new ClassPathResource("classpath:some.properties")
.getInputStream();
令人高兴的是,您可以使用classpath:
语法轻松地从XML连接InputStreams和资源:
The nice part is that you can wire both InputStreams and Resources easily from XML using the classpath:
syntax:
Java代码
private InputStream stream;
private Resource resource;
public void setStream(InputStream stream){
this.stream = stream;
}
public void setResource(Resource resource){
this.resource = resource;
}
属性接线:
<bean class="MyClass">
<property name="stream" value="classpath:file1.properties" />
<property name="resource" value="classpath:file2.properties" />
</bean>
如果您只想初始化静态的final字段,请按以下步骤操作:
If you just want to initialize a static final field, here's how to do it:
private static final String DOMAIN;
static{
InputStream inputStream=null;
try{
inputStream = new ClassPathResource("classpath:some.properties")
.getInputStream();
Properties props = new Properties();
props.load(inputStream);
String key = "your.property";
if(!props.containsKey(key))
throw new IllegalStateException("Property not found");
DOMAIN= props.getProperty(key);
} catch(IOException e){
throw new IllegalStateException(e);
}finally{
// apache commons / IO
IOUtils.closeQuietly(inputStream);
}
}
这篇关于如何从JavaBean(域类)的属性文件中读取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!