Apache Wicket: session injection dependency (using Guice)

I am using Wicket Auth / Roles and I am facing the same problem as OP this thread .

I need to access the DB service level in AuthenticatedWebSession (for user authentication). I followed Steve Flasby's suggestion and did the following:

 @Override public Session newSession(Request request, Response response) { Session s = new MySession(request); mInjector.inject(s); return s; } 

Unfortunately, this leads to

 java.lang.IllegalStateException: EntityManager is closed 

(presumably due to the fact that (a) I am using an open session in the view, and (b) the session spans multiple requests).

I solved this by moving the injection to the AuthenticatedWebSession.authenticate method.

 @Override public boolean authenticate(String username, String pass) { Injector.get().inject(this); ... } 

I suspect that this is not the best practice, because now I also need to access the service level in other methods, and it does not seem like a good idea to add Injector.get().inject(this) to each such method.

My question is:

How to inject into a session object for each request? (Or, if this is a bad approach all together, how do I access the service layer in AuthenticatedWebSession ?)

+4
source share
1 answer

You can implement IRequestCycleListener (expand AbstractRequestCycleListener ) and implement:

 @Override public void onBeginRequest(RequestCycle cycle) { if (Session.exists()) { Injector.get().inject(Session.get()); } } 

Register IRequestCycleListener in Application#init() with getRequestCycleListeners().add(new YourRequestCycleListener()) .

+5
source

Source: https://habr.com/ru/post/1487649/


All Articles