java - Jersey and AOP without Spring -
i using grizzly jersey 2.1. add aspect logging purpose (before , after each method execution). possibile without using spring or google-guice?
thanks
you can achieve similar using monitoring features offered jersey (particularly via event listeners). need create , register instance of applicationeventlistener , requesteventlistener listening resource_method_start , resource_method_finished events.
@provider public class myapplicationeventlistener implements applicationeventlistener { @override public void onevent(applicationevent event) { // noop. } @override public requesteventlistener onrequest(requestevent requestevent) { return new requesteventlistener() { @override public void onevent(final requestevent event) { switch (event.gettype()) { case resource_method_start: logmethod(event.geturiinfo(), true); break; case resource_method_finished: logmethod(event.geturiinfo(), false); break; } } private void logmethod(final extendeduriinfo uriinfo, final boolean entering) { final class<?> resource = uriinfo .getmatchedresources().get(0).getclass(); final method method = uriinfo .getmatchedresourcemethod() .getinvocable() .gethandlingmethod(); if (entering) { logger.entering(resource.tostring(), method.tostring()); } else { logger.exiting(resource.tostring(), method.tostring()); } } }; } }
register listener application:
public class myapplication extends application { @override public set<class<?>> getclasses() { final hashset<class<?>> classes = new hashset<class<?>>(); // add root resources. classes.add(helloworldresource.class); // add applicationeventlistener. classes.add(myapplicationeventlistener.class); return classes; } }
note: these events fired around resource method "to-be-executed" , not sub-resource locators might executed during resource matching phase.
edit 1
to log each invoked method (even methods invoked jax-rs resource method) refer aspectj project.
Comments
Post a Comment