如何从JavaBean(域类)中的属性文件中读取值?

问题描述:

我有一个域类,我想从属性文件(自动装配messageSource不会在这里工作)读取值,所以任何想法? 我使用spring,hibernate的 ,这里是一个示例:如何从JavaBean(域类)中的属性文件中读取值?

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; 
    } 

} 
+3

我不明白你的问题,为什么会自动布线无法正常工作。是因为它的实体类(因此超出了Spring AppContext的范围)。从属性文件中读取有什么特别之处。猜猜我有点失落。 – 2011-01-19 09:11:28

+0

我试过messageSource,但我总是得到异常org.hibernate.InstantiationException:无法实例化测试objectcom.myapp.domain.MyDomain和(ContextLoader。java:220) - 上下文初始化失败,java.lang.NullPointerException \t at com.myapp.domain.MyDomain。 – 2011-01-19 10:13:30

我还是不明白的问题,但我会假设你想从中设置bean属性一个属性文件。

其他的答案已经表明如何从一个属性文件(我将在下面显示的其他方式)Properties对象,我会告诉你如何使用Spring的BeanWrapper接口接线从它的属性:

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属性:

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); 
} 

参考:5.4. Bean manipulation and the BeanWrapper


实际上,Spring所指定的方式,从classpath中加载资源,使用the Resource mechanism

InputStream str = new ClassPathResource("classpath:some.properties") 
         .getInputStream(); 

的好处是,你可以很容易地从XML使用classpath:语法连线都InputStreams和资源:

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> 

如果你只是想初始化一个静态的最终场,这里是如何做到这一点:

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); 
    } 
} 

旁边的一切,你总是可以做,

Thread.currentThread.getContextClassLoader().getResourceAsStream("some.properties") 

但是,我还是好奇你想在你Entity读什么,从属性文件。

+0

我想读取属性的一些硬编码的默认值。这种方式和使用MessageSource有什么区别,这种方式有问题或什么? – 2011-01-19 10:10:39

+0

@ sword101:对不起,但我不知道'MessageSource`是什么。我认为可以这样做。 – 2011-01-19 10:22:25

添加到安萨里的代码

Properties p = new Properties(); 
p.load (Thread.currentThread().getContextClassLoader(). 
     getResourceAsStream("some.properties")); 

p.list(System.out); // or p.get("name") --> name=value. 

在网上搜索后,我发现下面

<!--Bean to load properties file --> 

<bean id="placeholderConfig" 
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
<property name="location" value="classpath:config.properties"> 
<!--reads config.properties file--> 

请阅读这篇文章http://www.zparacha.com/how-to-read-properties-file-in-spring/