gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:46:54 24 4
gpt4 key购买 nike

我有一个域类,我想从属性文件中读取值( Autowiring messageSource 在这里不起作用),所以有什么想法吗?我正在使用 spring、hibernate这是一个示例:

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

其他答案已经展示了如何从 .properties 文件获取 Properties 对象(我将在下面展示其他方法),我将向您展示如何使用 Spring 的 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 属性:

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 特有的从类路径加载资源的方法使用 the Resource mechanism

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

最好的一点是,您可以使用 classpath: 语法从 XML 轻松连接 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);
}
}

关于java - 如何从 JavaBean(域类)中的属性文件读取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4733401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com