Wicket AuthenticateWebSession 中的 EJB3 注入为 Null
我在应用程序中使用 Wicket + EJB3,遇到问题,但找不到任何相关主题,所以这里是:
我正在使用 Wicket 身份验证,并且需要在重写的方法验证中使用 EJB 中的方法(...)。
我可以在任何 wicket 页面中使用此 EJB,但是当涉及到 WebSession 时,它保持 Null,注入无法正常工作。
我的 WicketSession 类看起来像这样:
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;
}
}
我的 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) {
[...]
}
}
带有 Wicket 的 Web 部分打包在一个 war 中,带有 EJB 的业务部分打包在一个 jar 中,然后我将它部署在 JOnAS 服务器上。
任何帮助将不胜感激=)
尼古拉斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定注入可以与
一起使用IComponentInstantiationListener
(至少 Spring 版本是这样工作的)。 更新:确实如此,请参阅此文档。但是,会话不是组件,因此需要不同的机制。也许有一种方法可以在
Application.newSession()
方法?您必须查看JavaEEComponentInjector
的实现,并在创建会话时复制它的功能。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 yourSession
in theApplication.newSession()
method? You will have to take a look at the implementation ofJavaEEComponentInjector
and copy what it does when creating your session.正如 Sean Patrick Floyd 所指出的,会话不是组件,因此为 Wicket 组件提供的自动注入不适用。
在非组件中注入内容的常见习惯用法是将行添加
到构造函数中。
我还没有将其用于
WicketSession
扩展,但我不知道它不起作用的原因。在 JavaEEComponentInjector 中,注入方法几乎肯定会执行 JNDI 查找,您可以自己执行 JNDI 查找来获取对象,但这会重用现有的注入,并且如果您决定更改注入器(例如通过扩展 JavaEEComponentInjector),它可以确保您继续使用相同的注入。
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
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 extendingJavaEEComponentInjector
), it ensures that you'll continue to use the same injection.