Glassfish 自定义属性资源未从文件加载

发布于 2024-10-09 12:10:54 字数 1660 浏览 0 评论 0原文

我已经将一个 web 应用程序(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、类型为 java.util.Properties 的 JNDI 自定义资源,并使用工厂类 org.glassfish.resources。自定义.工厂.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”。

有什么想法吗?

I've deployed a webapp (war) to Glassfish v3 and I am trying to get it to read from properties defined in a custom resource.

In my app, I've defined the properties as:

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

and make use of it like this:

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);
        }
    }            
}

In Glassfish, I've created a JNDI Custom Resource called TestServletProperties of type java.util.Properties and using factory class org.glassfish.resources.custom.factory.PropertiesFactory. In the resource there is one property "fileName" with its value set to the absolute path of the properties file:

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

I've also tried

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

I have confirmed that the file exists and contains the referenced properties. Unfortunately, I'm getting back "null" for both values in my response.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福丶如此 2024-10-16 12:10:54

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

The solution is that you have to use the fully qualified "org.glassfish.resources.custom.factory.PropertiesFactory.fileName" versus just "fileName".

蘸点软妹酱 2024-10-16 12:10:54

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

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

希望这有帮助

The reason might be that you have a web.xml file with the header of a 2.4 (or older) servlet version.

@Resource and other annotations are only processed if you have at least version 2.5 in the header of web.xml. Be sure that you do not simply change the version but copy and paste the new header from somewhere as the namespace is different.

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文