我已经向Glassfish v3部署了一个webapp(war),我试图让它从自定义资源中定义的属性中读取。
在我的应用中,我将属性定义为:
@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”。
有什么想法吗?
答案 0 :(得分:1)
解决方案是您必须使用完全限定的" org.glassfish.resources.custom.factory.PropertiesFactory.fileName"与仅仅#34; fileName"。
答案 1 :(得分:0)
原因可能是您有一个带有2.4(或更旧版本)servlet版本标头的web.xml文件。
只有在web.xml的头文件中至少有2.5版本时才会处理@Resource和其他注释。请确保您不是简单地更改版本,而是从名称空间不同的地方复制并粘贴新标题。希望这有帮助