【问题标题】:Glassfish Custom Properties Resource Not Loading From FileGlassfish 自定义属性资源未从文件加载
【发布时间】:2010-12-29 21:14:30
【问题描述】:

我已将 webapp (war) 部署到 Glassfish v3,并试图让它从自定义资源中定义的属性中读取。

在我的应用中,我将属性定义为:

@Resource(mappedName = "TestServletProperties")
private Properties properties;

并像这样使用它:

protected void doGet(final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException,
        java.io.IOException
{
    String propertyOne = properties.getProperty("testServlet.propertyOne");
    String propertyTwo = properties.getProperty("propertyTwo");

    StringBuffer buffer = new StringBuffer("Properties Retrieved\n");
    buffer.append("Property One: " + propertyOne + "\n");
    buffer.append("Property Two: " + propertyTwo + "\n");

    try
    {
        response.getWriter().write(buffer.toString());
    }

    catch (Exception ex)
    {
        try
        {
            log.warn("Exception thrown", ex);
            response.getWriter().write(ex.getStackTrace().toString());
        }
        catch (IOException io)
        {
            log.warn("IOException thrown", io);
        }
    }            
}

在 Glassfish 中,我创建了一个名为 TestServletProperties 的 JNDI 自定义资源,类型为 java.util.Properties,并使用工厂类 org.glassfish.resources.custom.factory.PropertiesFactory。在资源中有一个属性“fileName”,其值设置为属性文件的绝对路径:

/Program Files/glassfishv3/glassfish/domains/domain1/applications/Test/WEB-INF/classes/TestServlet_lab.properties

我也试过

c:\Program Files\glassfishv3\glassfish\domains\domain1\applications\Test\WEB-INF\classes\TestServlet_lab.properties

我已确认该文件存在并包含引用的属性。不幸的是,我在回复中的两个值都返回“null”。

有什么想法吗?

【问题讨论】:

标签: properties jndi glassfish-3


【解决方案1】:

解决方案是您必须使用完全限定的“org.glassfish.resources.custom.factory.PropertiesFactory.fileName”而不是“fileName”。

【讨论】:

    【解决方案2】:

    原因可能是您有一个带有 2.4(或更早)版本的 servlet 标头的 web.xml 文件。

    @Resource 和其他注释只有在 web.xml 的标头中至少有 2.5 版本时才会被处理。确保您不是简单地更改版本,而是从某处复制并粘贴新标头,因为命名空间不同。

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2013-12-19
      • 2012-03-10
      • 2021-04-03
      • 2011-06-22
      相关资源
      最近更新 更多