问题描述
我要初始化Jersey Rest服务并引入一个全局应用程序范围的变量,该变量应在应用程序启动时进行计算,并且应在每个rest资源和每种方法中可用(此处由整数globalAppValue = 17表示,但稍后会成为一个复杂的对象.
I want to initialize a Jersey Rest service and introduce a global application-wide variable which should be calculated at application start up-time and should be available in each rest resource and each method (here indicated by the integer globalAppValue=17, but will be a complex object later).
为了初始化服务并在启动时计算一次值,我发现了两种做法:常规的ServletContextListener和Jersey ResourceConfig方法.但是我不明白两者之间的区别是什么?两种方法都在启动时触发(均打印System.out-messages).
In order to initialize the service and calculate the value once at start up I found two practices: The general ServletContextListener and the Jersey ResourceConfig method. But I have not understood what is the difference between both of them? Both methods fire at start up (both System.out-messages are printed).
这是我的ServletContextListener的实现,可以正常运行:
Here is the implementation of my ServletContextListener which works fine:
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);
}
}
这是Jersey REST ResourceConfig方法的实现,其中ServletContext不可用.此应用程序对象后来也无法通过注入资源方法来使用:
And this is the implementation of the Jersey Rest ResourceConfig-method in which the ServletContext is not available. Neither is this Application-object later availabe by injection with the resource-methods:
@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;
}
}
这是我想在资源方法中获得全局值的方式:
This is the way I would like to gain access in the resource methods to the global value:
@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中,所以会更喜欢这种方式.所以我的问题是哪种方法将是最佳实践.谢谢!
So while the classic ServletContextListener works fine I got several problems to use the ResourceConfig/Application, but would prefer this way because it seems to integrate more natively into Jersey. So my question is which way would be the best practice to use. Thanks!
推荐答案
只需调用 property( key, value )
.
public MyApplication() {
property("MyProp", "MyValue");
}
在您的资源类中,只允许您注入超抽象类 javax.ws.rs.core.Application
,它是ResourceConfig
的扩展名.
In your resource class, your are only allowed to inject the super abstract class javax.ws.rs.core.Application
, which ResourceConfig
extends from.
然后您可以做的是调用标准的Application
API方法之一来获取设置属性.该方法当然被命名为 getProperties()
,它返回属性映射.
Then what you can do is call one of standard the Application
API methods to get set properties. That method of course is named getProperties()
, which returns a map of properties.
@Path("/")
public class TestResource
{
@Context
Application application;
@GET
public String get() {
String value = (String)application.getProperties().get("MyProp");
}
}
还通过在ResourceConfig
上使用property
方法,将该属性放入全局 javax.ws.rs.core.Configuration
对象,该对象也是可注入的.因此,您可以注入Configuration
Also by using the property
method on the ResourceConfig
, that property is put into a global javax.ws.rs.core.Configuration
object, which is also injectable. So instead of Application
, you could inject Configuration
@Path("/")
public class TestResource
{
@Context
Configuration config;
@GET
public String get() {
String value = (String)config.getProperty("MyProp");
}
}
另请参见:
- 使用自定义hk2 InjectionResolver进行注入应用程序配置,以其他一些有趣的方式仅注入值,而不必从
Application
或Configuration
检索它
- Using a custom hk2 InjectionResolver to inject application configuration for some other interesting ways to inject just the value instead of having to retrieve it from the
Application
orConfiguration
这篇关于Jersey REST Service的ResourceConfig和ServletContextListener之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!