我的@inject注释有问题,无法实例化我的服务。我没有成功地遵循每个教程和官方文档。请你帮我解决一下我的问题。
我的控制员:`
@Named
@ManagedBean
@RequestScoped
public class LoginAction {
private Logger logger = LoggerFactory.getLogger( LoginAction.class );
@Size(min = 5 , message = "#{msg.auth_txtLog_minLeng}")
private String txtUserName;
@Size(min = 5 , message = "#{msg.auth_txtPwd_minLeng}")
private String txtPassWord;
@Inject
private UserService userService;
我的服务叫我的DAO
@Named
public class UserServiceImpl implements UserService {
@Inject
UserDao userDao;
我得到一个nullPointerException:
> ATTENTION: #{loginAction.logIn}: java.lang.NullPointerException
javax.faces.FacesException: #{loginAction.logIn}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 23 more
Caused by: java.lang.NullPointerException
at ma.trk.sgpa.controller.LoginAction.logIn(LoginAction.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 24 more
6 déc. 2013 11:37:43 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/sgpa-jsf] threw exception [java.lang.NullPointerException] with root cause
答案 0 :(得分:1)
Stacktrace说你正在使用Tomcat,如org.apache.tomcat/coyote/catalina.*
行所示。但是,Tomcat作为一个准系统JSP / Servlet容器本身不支持CDI,它必须单独安装(就像你为JSF所做的那样)。
Stacktrace还表示bean不是由@Named
管理,而是由@ManagedBean
管理,正如完全没有根本原因的CDI代理类签名所示。如果确实使用了@Named
,那么当Weld用作CDI实现时,您会看到如下额外的行:
Caused by: java.lang.NullPointerException
at ma.trk.sgpa.controller.LoginAction.logIn(LoginAction.java:35)
--> at ma.trk.sgpa.controller.LoginAction$Proxy$_$$_WeldClientProxy.logIn(LoginAction$Proxy$_$$_WeldClientProxy.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
另外,您的评论
仍然无法正常工作。如果我删除@ManagedBean我的页面Xhtml不知道我的字段
确认CDI未正确安装。同样令人惊讶的是,你没有明确提到你是如何安装它的,因为Tomcat + CDI是一个特例,它证实你实际上不知道你在做什么。也许您只是在@Named
和@Inject
上遇到了编译错误,您只是盲目地下载/移动/复制随机JAR文件,不小心尝试解决这些编译错误,而没有真正阅读CDI实现的文档。配置Tomcat。
前往以下答案,了解如何让CDI在Tomcat中运行:How to install and use CDI on Tomcat?。