我想初始化Jersey Rest服务并引入应用程序启动时应计算的全局应用程序范围的变量,并且应该在每个其余资源和每个方法中都可用(这里用整数globalAppValue = 17表示,但稍后会是一个复杂的对象)。Jersey Rest服务的ResourceConfig和ServletContextListener之间的区别
为了初始化服务并在启动时计算一次值,我发现了两种做法:一般ServletContextListener和Jersey ResourceConfig方法。但我还没有明白他们之间的区别是什么?两种方法都在启动时触发(两个System.out消息都被打印)。
这里是我的ServletContextListener的执行工作正常:
public class LoadConfigurationListener implements ServletContextListener
{
private int globalAppValue = 17;
@Override
public void contextDestroyed (ServletContextEvent event)
{
}
@Override
public void contextInitialized (ServletContextEvent event)
{
System.out.println ("ServletContext init.");
ServletContext context = event.getServletContext();
context.setAttribute ("globalAppValue", globalAppValue);
}
}
这是泽西休息ResourceConfig-方法,其中ServletContext的不可执行。无论是后来通过注射资源的方法速效此应用对象:
@ApplicationPath("Resources")
public class MyApplication extends ResourceConfig
{
@Context
ServletContext context;
private int globalAppValue = 17;
public MyApplication() throws NamingException
{
System.out.println ("Application init.");
// returns NullPointerException since ServletContext is not injected
context.setAttribute ("globalAppValue", 17);
}
public int getAppValue()
{
return globalAppValue;
}
}
这是我想在资源方法,以全局值获得方式:
@Path("/")
public class TestResource
{
@Context
ServletContext context;
@Context
MyApplication application;
@Path("/test")
@GET
public String sayHello() throws SQLException
{
String result = "Hello World: ";
// returns NullPointerException since application is not injected
result += "globalAppValue=" + application.getAppValue();
// works!
result += "contextValue=" + context.getAttribute ("globalAppValue");
return result;
}
}
所以虽然经典的ServletContextListener可以正常工作,但我使用ResourceConfig/Application时遇到了一些问题,但会更喜欢这种方式,因为它似乎更加原生地集成到了Jersey中。所以我的问题是使用哪种方式是最佳做法。谢谢!
Thanx,这种方式的作品!但是在泽西休息网络服务中哪种方法更受欢迎? ServletContextListener或Application-ResourceConfig方式? –
我更喜欢与泽西岛坚持。如果你不需要,不需要使用servlet容器。它就像穷人的依赖注入。泽西岛拥有处理DI的所有能力。 –
我似乎又被卡住了:当我使用'property(“value”,17);'在公共类Application的构造函数中扩展ResourceConfig'时,我可以立即重读属性,但不是在我的Resource-method中。有注入'@Context应用程序应用程序;'工作(应用程序不为空),但application.getProperties()返回null。那么在Application类中是否有特殊的注释?您能否将您上一个示例中的Application类扩展为带有注释和导入的完整类? –