简体   繁体   English

有没有办法从Java Bean访问web.xml属性?

[英]Is there a way to access web.xml properties from a Java Bean?

Is there any way in the Servlet API to access properties specified in web.xml (such as initialization parameters) from within a Bean or Factory class that is not associated at all with the web container? Servlet API中是否有任何方法可以从与Web容器完全无关的Bean或Factory类中访问web.xml中指定的属性(例如初始化参数)?

For example, I'm writing a Factory class, and I'd like to include some logic within the Factory to check a hierarchy of files and configuration locations to see which if any are available to determine which implementation class to instantiate - for example, 例如,我正在编写一个Factory类,我想在Factory中包含一些逻辑来检查文件和配置位置的层次结构,以查看哪些可用于确定实例化哪个实现类 - 例如,

  1. a properties file in the classpath, 类路径中的属性文件,
  2. a web.xml parameter, 一个web.xml参数,
  3. a system property, or 系统属性,或
  4. some default logic if nothing else is available. 一些默认逻辑,如果没有别的可用。

I'd like to be able to do this without injecting any reference to ServletConfig or anything similiar to my Factory - the code should be able to run ok outside of a Servlet Container. 我希望能够在不注入ServletConfig任何引用或类似于我的Factory的任何内容的情况下执行此操作 - 代码应该能够在Servlet容器之外运行。

This might sound a little bit uncommon, but I'd like for this component I'm working on to be able to be packaged with one of our webapps, and also be versatile enough to be packaged with some of our command-line tools without requiring a new properties file just for my component - so I was hoping to piggyback on top of other configuration files such as web.xml. 这可能听起来有点不常见,但是我想要这个组件,我正在努力与我们的一个webapp一起打包,并且还具有足够的通用性,可以与我们的一些命令行工具一起打包需要一个新的属性文件只为我的组件 - 所以我希望搭载其他配置文件,如web.xml。

If I recall correctly, .NET has something like Request.GetCurrentRequest() to get a reference to the currently executing Request - but since this is a Java app I'm looking for something simliar that could be used to gain access to ServletConfig . 如果我没记错的话,.NET有类似Request.GetCurrentRequest()来获取对当前正在执行的Request的引用 - 但由于这是一个Java应用程序,我正在寻找可以用来获取ServletConfig访问权限的东西。

One way you could do this is: 你可以这样做的一种方法是:

public class FactoryInitialisingServletContextListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent event) {
    }

    public void contextInitialized(ServletContextEvent event) {
        Properties properties = new Properties();
        ServletContext servletContext = event.getServletContext();
        Enumeration<?> keys = servletContext.getInitParameterNames();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            String value = servletContext.getInitParameter(key);
            properties.setProperty(key, value);
        }
        Factory.setServletContextProperties(properties);
    }
}

public class Factory {

    static Properties _servletContextProperties = new Properties();

    public static void setServletContextProperties(Properties servletContextProperties) {
        _servletContextProperties = servletContextProperties;
    }
}

And then have the following in your web.xml 然后在您的web.xml中包含以下内容

<listener>
    <listener-class>com.acme.FactoryInitialisingServletContextListener<listener-class>
</listener>

If your application is running in a web container, then the listener will be invoked by the container once the context has been created. 如果您的应用程序在Web容器中运行,则在创建上下文后,容器将调用该侦听器。 In which case, the _servletContextProperties will be replaced with any context-params specified in the web.xml. 在这种情况下,_servletContextProperties将替换为web.xml中指定的任何context-params。

If your application is running outside a web container, then _servletContextProperties will be empty. 如果您的应用程序在Web容器外部运行,则_servletContextProperties将为空。

Have you considered using the Spring framework for this? 您是否考虑过使用Spring框架? That way, your beans don't get any extra cruft, and spring handles the configuration setup for you. 这样,你的bean不会得到任何额外的瑕疵,spring会为你处理配置设置。

I think that you will have to add an associated bootstrap class which takes a reference to a ServletConfig (or ServletContext) and transcribes those values to the Factory class. 我认为您必须添加一个关联的引导类,它引用ServletConfig(或ServletContext)并将这些值转录到Factory类。 At least this way you can package it separately. 至少这种方式你可以单独打包它。

@toolkit : Excellent, most humbled - This is something that I have been trying to do for a while @toolkit:非常好,最谦卑 - 这是我一直试图做的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM