java - Difference between ResourceConfig and ServletContextListener for Jersey Rest Service -
i want initialize jersey rest service , introduce global application-wide variable should calculated @ application start up-time , should available in each rest resource , each method (here indicated integer globalappvalue=17, complex object later).
in order initialize service , calculate value once @ start found 2 practices: general servletcontextlistener , jersey resourceconfig method. have not understood difference between both of them? both methods fire @ start (both system.out-messages printed).
here implementation of servletcontextlistener 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); } }
and implementation of jersey rest resourceconfig-method in servletcontext not available. neither application-object later availabe injection 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 not injected context.setattribute ("globalappvalue", 17); } public int getappvalue () { return globalappvalue; } }
this way gain access in resource methods 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 not injected result += "globalappvalue=" + application.getappvalue (); // works! result += "contextvalue=" + context.getattribute ("globalappvalue"); return result; } }
so while classic servletcontextlistener works fine got several problems use resourceconfig/application, prefer way because seems integrate more natively jersey. question way best practice use. thanks!
you set property in resourceconfig
calling property( key, value )
.
public myapplication() { property("myprop", "myvalue"); }
in resource class, allowed inject super abstract class javax.ws.rs.core.application
, resourceconfig
extends from.
then can call 1 of standard application
api methods set properties. method of course named getproperties()
, returns map of properties.
@path("/") public class testresource { @context application application; @get public string get() { string value = (string)application.getproperties().get("myprop"); } }
also using property
method on resourceconfig
, property put global javax.ws.rs.core.configuration
object, injectable. instead of application
, inject configuration
@path("/") public class testresource { @context configuration config; @get public string get() { string value = (string)config.getproperty("myprop"); } }
see also:
- using custom hk2 injectionresolver inject application configuration other interesting ways inject value instead of having retrieve
application
orconfiguration
Comments
Post a Comment