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

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;

}

}

解决方案

I still don't understand the question, but I'll assume that you want to set bean properties from a properties file.

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 entry:props.entrySet()){

String propertyName = entry.getKey().toString();

if(wrapper.isWritableProperty(propertyName)){

wrapper.setPropertyValue(propertyName, entry.getValue());

}

}

}

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

}

Actually, the Spring-specific ways to load resources from the classpath use the Resource mechanism

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

.getInputStream();

The nice part is that you can wire both InputStreams and Resources easily from XML using the classpath: syntax:

Java Code

private InputStream stream;

private Resource resource;

public void setStream(InputStream stream){

this.stream = stream;

}

public void setResource(Resource resource){

this.resource = resource;

}

property wiring:

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值