在推土机映射中访问环境变量

时间:2012-06-13 16:44:17

标签: dozer

我想在dozer映射文件中访问我的环境变量。有办法吗?我看了官方文档,他们列出了变量的用法。但它们不是环境变量。还有办法在dozer映射文件中访问.property文件值吗?

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用类工厂并创建类的新实例并传入所需的环境变量。请参阅文档。 http://dozer.sourceforge.net/documentation/custombeanfactories.html

public class EnvironmentSetterBeanFactory implements BeanFactory {

public Object createBean(Object source, Class<?> sourceClass, String targetBeanId) {

try {
    Class<?> targetClass;
    targetClass = Class.forName(targetBeanId);
    Object instance = targetClass.newInstance()
    if (instance instanceof YourClass) {
        YourClass yourClass = (YourClass) idSource;
        yourClass.setThatVar(System.getenv("myenvvar"));            
    }
    return instance;
} catch (InstantiationException e) {
    throw new RuntimeException(e);
} catch (IllegalAccessException e) {
    throw new RuntimeException(e);
}
    }

} catch (ClassNotFoundException e) {
    throw new RuntimeException(e);
}

}