sách gpt4 ai đã đi

file - access to file to files tomcat的conf文件夹下的一个文件

In lại 作者:行者123 更新时间:2023-11-28 22:22:58 28 4
mua khóa gpt4 Nike

我想知道是否可以访问放在 tomcat 的 conf 文件夹中的文件。通常我会在这个文件中放置多个 webapp 的配置,在 war 之外。

我想使用类路径独立于文件系统。

我过去使用过 lib 文件夹。效果很好。但是用lib文件夹放conf文件有点没意义。

有人可以帮我解决这个问题吗?

1 Câu trả lời

我见过很多人们在 web 应用程序中进行配置的错误方式,这些方式要么使它不是真正的配置(当您更改配置时必须重新部署/发布),要么灵 active 很小。

我如何解决这个问题是将 Spring 用于 property placeholder但通常你需要引导 Spring 或任何你的 MVC 堆栈,然后再加载一个属性,该属性说明加载配置的位置。我为此使用了一个监听器:

package com.evocatus.util;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;


public class SimpleContextListenerConfig /*extend ResourceBundle */ implements ServletContextListener{

private ServletContext servletContext;

@Ghi đè
public void contextInitialized(ServletContextEvent sce) {
servletContext = sce.getServletContext();
servletContext.setAttribute(getClass().getCanonicalName(), this);
}

@Ghi đè
public void contextDestroyed(ServletContextEvent sce) {

}

public static String getProperty(ServletContext sc, String propName, String defaultValue) {
SimpleContextListenerConfig config = getConfig(sc);
return config.getProperty(propName, defaultValue);
}

public static SimpleContextListenerConfig getConfig(ServletContext sc) {
SimpleContextListenerConfig config =
(SimpleContextListenerConfig) sc.getAttribute(SimpleContextListenerConfig.class.getCanonicalName());
return config;
}

public String getProperty(String propName, String defaultValue)
{
/*
* TODO cache properties
*/
String property = null;

if (property == null)
property = servletContext.getInitParameter(propName);
if (property == null)
System.getProperty(propName, null);
//TODO Get From resource bundle
if (property == null)
property = defaultValue;

return property;
}
}

https://gist.github.com/1083089

属性将首先从 servlet 上下文中提取,然后是系统属性,因此允许您覆盖某些 Web 应用程序。您可以通过更改 web.xml(不推荐)或 creating a context.xml 来更改 certian webapp 的配置。

您可以使用静态方法获取配置:

public static SimpleContextListenerConfig getConfig(ServletContext sc);

关于file - access to file to files tomcat的conf文件夹下的一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695935/

28 4 0
Bài viết được đề xuất: ios - 如何在 iOS 中刷新页脚 View ?
Bài viết được đề xuất: tomcat - 登录嵌入式 Tomcat
Bài viết được đề xuất: java - Servlet 需要访问嵌入在 war 文件中的 SQLite DB
Bài viết được đề xuất: java - 在 Eclipse 中本地调试 Web 服务
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com