Properties Glassfish自定义属性资源未从文件加载
我已经在Glassfish v3上部署了一个webapp(war),我正在尝试从自定义资源中定义的属性读取它 在我的应用程序中,我将属性定义为:Properties Glassfish自定义属性资源未从文件加载,properties,jndi,glassfish-3,Properties,Jndi,Glassfish 3,我已经在Glassfish v3上部署了一个webapp(war),我正在尝试从自定义资源中定义的属性读取它 在我的应用程序中,我将属性定义为: @Resource(mappedName = "TestServletProperties") private Properties properties; 并像这样利用它: protected void doGet(final HttpServletRequest request, final HttpServletResponse
@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中,我使用工厂类org.Glassfish.resources.Custom.factory.PropertiesFactory创建了一个名为TestServletProperties的JNDI自定义资源,类型为java.util.Properties。在资源中有一个属性“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”
有什么想法吗?原因可能是您有一个web.xml文件,其标题是2.4(或更高版本)的servlet版本 @仅当web.xml的标题中至少有版本2.5时,才会处理资源和其他注释。确保您不只是更改版本,而是从某个地方复制并粘贴新的头,因为名称空间不同
希望这对您有所帮助。解决方案是您必须使用完全限定的“org.glassfish.resources.custom.factory.PropertiesFactory.fileName”而不是“fileName”。我为这个问题创建了一个bug: