2011-01-19 68 views
0

我有一個域類,我想從屬性文件(自動裝配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

回答

3

我還是不明白的問題,但我會假設你想從中設置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); 
    } 
} 
0

旁邊的一切,你總是可以做,

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

但是,我還是好奇你想在你Entity讀什麼,從屬性文件。

+0

我想讀取屬性的一些硬編碼的默認值。這種方式和使用MessageSource有什麼區別,這種方式有問題或什麼? – 2011-01-19 10:10:39

+0

@ sword101:對不起,但我不知道'MessageSource`是什麼。我認爲可以這樣做。 – 2011-01-19 10:22:25

0

添加到安薩里的代碼

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/