简体   繁体   中英

EJB3 injection is Null in Wicket AuthenticateWebSession

I'm using Wicket + EJB3 in an application and I face a problem but I can't find any topic related, so here it is:

I'm using Wicket authentication, and need to use methods from an EJB in the overrided methods authenticate(...).

I can use this EJB in any wicket page, but when it comes to the WebSession, it stays Null, the injection is not working somehow.

My WicketSession class looks something like this:

public class WicketSession extends AuthenticatedWebSession {

   @EJB(name = "UserService")
   private UserService userService;

   private User user = null;

   public WicketSession(Request request) {
    super(request);
   }

   @Override
   public boolean authenticate(final String login, final String password) {

      user = userService.findByLoginPwd(login, password);

      return user != null;;
   }

   public User getUser() {
      return user;
   }

   public void setUser(User user) {
      this.user = user;
   }
}

And my EJB3:

@Remote
public interface UserService {
    public User findByLoginPwd(final String login, final String pwd);
}

@Stateless
public class UserServiceImpl implements UserService {

   public User findByLoginPwd(final String login, final String pwd) {
       [...]
   }
}

The Web part with Wicket is packaged in a war, the business part with EJBs is packaged in a jar, and then I make a ear to deploy it on a JOnAS server.

Any help would be highly appreciated =)

Nicolas

I'm pretty sure the injection works with an IComponentInstantiationListener (at least that's how the Spring version works). Update: it does, see this document .

However, Sessions are not components, so a different mechanism is needed. Perhaps there is a way to wire your Session in the Application.newSession() method? You will have to take a look at the implementation of JavaEEComponentInjector and copy what it does when creating your session.

As Sean Patrick Floyd noted, Sessions are not components, so the automatic injection supplied for Wicket components doesn't apply.

A common idiom for injecting stuff in a non-component is to add the line

    InjectorHolder.getInjector().inject(this);

to the constructor.

I haven't used this for a WicketSession extension, but I don't know of a reason it won't work.

In JavaEEComponentInjector , the inject method is almost certainly doing a JNDI lookup, and you could do a JNDI lookup yourself to get the object, but this is reusing the existing injection, and if you decide to change injectors (say by extending JavaEEComponentInjector ), it ensures that you'll continue to use the same injection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM