Java Web应用程序和属性文件

时间:2015-10-23 08:13:19

标签: java web-services properties jersey

我正在尝试使用Java Jersey创建一个Web应用程序。该项目(动态Web项目)应该在运行时期间由客户端/服务器管理员更改参数。管理员无法访问数据库。 在将我们的应用程序转换为Web服务之前,我们在主jar旁边的文件夹中定义了属性文件。有没有办法在动态网络应用程序中拥有类似的东西? 我的想法是拥有一个包含属性文件的文件夹,服务器管理员可以访问和修改(这样Web服务就可以在运行时更改它的工作参数)。在数据库而不是文件中存储属性是不行的。它必须是配置文件:) 您是否知道如何将我的属性文件放在相对路径中,以便使用该文件的服务器管理员可以在WS已部署并正常工作时更改某些Web服务参数?

1 个答案:

答案 0 :(得分:0)

根据您的要求,您可以使用属性文件,但事情是WS仅在启动时加载属性文件。因此,您必须在程序代码中加载该属性文件。并且您需要在一段时间间隔后重新加载文件。   请参考以下代码供您参考:

公共类ResourceHandler {

/** The service end points. Creates properties file object. */
Properties serviceEndPoints = new Properties();


/** The resource handler. */
private static ResourceHandler resourceHandler = new ResourceHandler();

/**
 * Instantiates a new resource handler.
 */
ResourceHandler() {
    try {
           /* Load the properties file from class path, you can use reguler file path instead of this. */
        serviceEndPoints.load(this.getClass().getResourceAsStream(
                "serviceEndPoints.properties"));

    } catch (IOException e) {
    }
}

/**
 * Instance.
 * 
 * @return the resource handler
 */
public static synchronized ResourceHandler instance() {
    if (resourceHandler == null)
        resourceHandler = new ResourceHandler();
    return resourceHandler;
}

/**
 * Gets the service end points.
 * 
 * @param key
 *            the key
 * @return the service end points
 * Use this method to get values of parameters from properties file.
 */
public String getServiceEndPoints(String key) {
    return ((String) serviceEndPoints.get(key));
}

}

以上代码用于一次加载文件。由于您想要获取那些已立即更改的参数,因此您必须在一段时间间隔后加载该文件。但肯定有可能!