【问题标题】:jax-rs rest service calling properties filesjax-rs 休息服务调用属性文件
【发布时间】:2017-04-05 01:39:04
【问题描述】:

上下文

我按照SO answer 创建并加载了一个属性文件。

我的后端代码分为 4 个项目(服务、业务、DAO、模型)

服务

@Path("/users")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers() {
    return UserBusiness.getUsers();
}

商业

public List<User> getUsers(){
    return _userDao.findAll();
}

DAO

public List<User> getUsers(){
    try{
        String query = "";
        PreparedStatement ps = null;
        //Query to DB here
    }catch(SQLException e){
    }
}

模型

public class User{
    private int id;
    private String username;
    private String password;
    private String fullName;
}

我的属性文件存储在包com.resources下的Services项目中,包com.service中的这个项目内的类ApplicationConfig包含这个

public static final String PROPERTIES_FILE = "config.properties";
public static Properties properties = new Properties();

private Properties readProperties() {
    InputStream inputStream = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
    if (inputStream != null) {
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            logger.severe(e.getMessage());
        }
    }
    return properties;
}

问题

我的 Services 项目包含项目 Models 作为依赖项。

我需要在我的DAO 项目中检索一个属性值(也许稍后在其他项目中)。我无法将项目 Services 添加到我的 DAO 项目中,因为它会添加循环依赖项。因此我无法联系到ApplicationConfig.properties.getProperty("myprop")

如何使用属性文件?我应该让readProperties() 里面ApplicationConfig 吗?我应该将属性文件放在哪些项目中?

【问题讨论】:

    标签: java web-services rest properties-file


    【解决方案1】:

    理想情况下,您的 DAO 项目不应依赖于与服务项目相同的文件上的配置。你有两个选择:

    • 将您的服务项目中的所有配置一次加载到一个类中,然后再加载。在这种情况下,您最终会得到一个包含所有项目配置的大配置文件。

    • 在单独项目的 util 类中的静态方法中重用配置文件加载器代码。现在每个项目都可以拥有自己的配置文件,并依赖 util 项目来加载和读取配置文件。

    【讨论】:

    • 如果我使用第二种方法意味着每个项目 1 个配置文件。配置文件不会在服务器启动时加载,但每次我阅读它们时?我对吗 ?他们是更好的方法吗?
    • 在第一次读取时使用单例模式存储所有属性。这样,所有后续调用都不需要每次都读取额外的文件。
    【解决方案2】:

    我在我的DAO 项目中使用了单例。每个项目我将有 1 个config.properties 具有以下课程

    public class PropertyHandler {
    
        private final Logger logger = Logger.getLogger(PropertyHandler.class.getName());
    
        private static PropertyHandler instance = null;
    
        private Properties props = null;
    
        private PropertyHandler() {
            try {
                props = new Properties();
                InputStream  in = PropertyHandler.class.getResourceAsStream("config.properties");
                props.load(in);
                in.close();
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Error when loading DAO properties file\n***\n{0}", e.getMessage());
            }
    
        }
    
        public static synchronized PropertyHandler getInstance() {
            if (instance == null) {
                instance = new PropertyHandler();
            }
            return instance;
        }
    
        public String getValue(String propKey) {
            return this.props.getProperty(propKey);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-27
      • 2016-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多