gpt4 book ai didi

java - CDI @inject 无法实例化我的服务

转载 作者:行者123 更新时间:2023-12-01 13:45:01 25 4
gpt4 key购买 nike

我的 @inject 注释有问题,它无法实例化我的服务。我遵循每个教程和官方文档但没有成功。请你帮我解决我的问题。

我的 Controller :`

@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

最佳答案

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)
...

还有你的评论

still not working. if i remove @ManagedBean my page Xhtml don't know my fields

确认 CDI 根本没有正确安装。同样令人惊讶的是,您没有在任何地方明确提及您到底是如何安装它的,因为 Tomcat+CDI 是一个特例,这证实您实际上不知道自己在做什么。也许您刚刚遇到了 @Named@Inject 上的编译错误,并且您只是盲目地下载/移动/复制了随机 JAR 文件,粗心地尝试解决这些编译错误,而实际上并没有这样做。阅读 CDI 实现文档来配置 Tomcat。

前往以下答案,了解如何让 CDI 在 Tomcat 中工作:How to install and use CDI on Tomcat? .

关于java - CDI @inject 无法实例化我的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20422390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com