【发布时间】: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