gpt4 book ai didi

java - 无法在 jboss 7 上使用 @EJB 注入(inject) EJB 3

转载 作者:行者123 更新时间:2023-12-01 12:43:40 27 4
gpt4 key购买 nike

I am trying to use @EJB annotation to call a stateless bean. When I print the value of the reference, it is NULL I a'm running on jboss 7. Please help me I have traied all possipole configruation, If I missed any thing please notify me. find the full stack below                     

Jboss 日志:-

JBAS015876:开始部署“medmark.war” 17:33:56,948 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor](MSC 服务线程 1-8)部署单元部署“medmark.war”中名为 HelloService 的 session bean 的 JNDI 绑定(bind)如下:

    java:global/medmark/HelloService!com.ntgclarity.medmark.service.HelloService
java:app/medmark/HelloService!com.ntgclarity.medmark.service.HelloService
java:module/HelloService!com.ntgclarity.medmark.service.HelloService
java:global/medmark/HelloService
java:app/medmark/HelloService
java:module/HelloService

17:33:57,374 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)
17:33:57,388 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010404: Deploying non-JDBC-compliant driver class com.mysql.fabric.jdbc.FabricMySQLDriver (version 5.1)
17:33:57,580 INFO [javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-6) Initializing Mojarra 2.1.7-jbossorg-1 (20120227-1401) for context '/medmark'
17:33:58,651 INFO [org.hibernate.validator.util.Version] (MSC service thread 1-6) Hibernate Validator 4.2.0.Final
17:33:59,582 INFO [org.primefaces.webapp.PostConstructApplicationEventListener] (MSC service thread 1-6) Running on PrimeFaces 5.0
17:33:59,627 INFO [org.jboss.web] (MSC service thread 1-6) JBAS018210: Registering web context: /medmark
17:33:59,644 INFO [org.jboss.as] (MSC service thread 1-7) JBAS015951: Admin console listening on http://0.0.0.0:9990
17:33:59,652 INFO [org.jboss.as] (MSC service thread 1-7) JBAS015874: JBoss AS 7.1.1.Final "Brontes" started in 8464ms - Started 349 of 476 services (126 services are passive or on-demand)
17:33:59,953 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "medmark.war"
17:34:01,423 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http-localhost-127.0.0.1-8080-1) Error Rendering View[/portal/login.xhtml]: com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.ntgclarity.medmark.managedbean.HelloBean.
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.7-jbossorg-2.jar:]
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.parser.AstValue.getValue(AstValue.java:147) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.13.Final.jar:]
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIOutput.getValue(UIOutput.java:169) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:75) [primefaces-5.0.jar:5.0]
at org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:71) [primefaces-5.0.jar:5.0]
at org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:52) [primefaces-5.0.jar:5.0]
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.event(JBossWebContext.java:67)
at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.invoke(JBossWebContext.java:48)
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_45]
Caused by: java.lang.NullPointerException
at com.ntgclarity.medmark.managedbean.HelloBean.<init>(HelloBean.java:59) [classes:]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_45]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_45]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_45]
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_45]
at java.lang.Class.newInstance0(Class.java:357) [rt.jar:1.6.0_45]
at java.lang.Class.newInstance(Class.java:310) [rt.jar:1.6.0_45]
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188) [jsf-impl-2.1.7-jbossorg-2.jar:]
... 42 more

17:34:01,781 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/medmark].[facesServlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet facesServlet threw exception: java.lang.NullPointerException
at com.ntgclarity.medmark.managedbean.HelloBean.<init>(HelloBean.java:59) [classes:]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_45]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_45]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_45]
at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_45]
at java.lang.Class.newInstance0(Class.java:357) [rt.jar:1.6.0_45]
at java.lang.Class.newInstance(Class.java:310) [rt.jar:1.6.0_45]
at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.7-jbossorg-2.jar:]
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.parser.AstValue.getValue(AstValue.java:147) [jbossweb-7.0.13.Final.jar:]
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.13.Final.jar:]
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIOutput.getValue(UIOutput.java:169) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:75) [primefaces-5.0.jar:5.0]
at org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:71) [primefaces-5.0.jar:5.0]
at org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:52) [primefaces-5.0.jar:5.0]
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.7-jbossorg-2.jar:]
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139) [jsf-impl-2.1.7-jbossorg-2.jar:]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594) [jboss-jsf-api_2.1_spec-2.0.1.Final.jar:2.0.1.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.event(JBossWebContext.java:67)
at org.jboss.modcluster.container.jbossweb.JBossWebContext$RequestListenerValve.invoke(JBossWebContext.java:48)
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:567) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_45]

pom.xml
<properties>
<!-- API Versions -->
<java-version>1.6</java-version>
<!-- Settings -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework-version>4.0.5.RELEASE</org.springframework-version>
<org.spring.security-version>3.1.4.RELEASE</org.spring.security-version>
<com.sun.faces>2.2.4</com.sun.faces>
<primefaces.version>5.0</primefaces.version>
</properties>

<repositories>
<!--PRIMEFACES REPOSITORY -->
<repository>
<id>prime-repo</id>
<name>Prime Repo</name>
<url>http://repository.primefaces.org</url>
</repository>

</repositories>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

<!-- MySQL CONNECTOR -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>

<!-- JAVA SERVER FACES JSF API -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${com.sun.faces}</version>
<scope>provided</scope>
</dependency>

<!-- JAVA SERVER FACES JSF Imp -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${com.sun.faces}</version>
<scope>provided</scope>
</dependency>

<!-- JAVA JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<!-- JAVA SERVLET -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

<!-- PRIMEFACES 5 -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>${primefaces.version}</version>
</dependency>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>medmark</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
</plugins>

</build>

EJB 类:

    @Stateless
public class HelloService{

public HelloService(){

}
public String sayHello() {
return "Hello EJB";
}
}

JSF bean :

    @ManagedBean
@RequestScoped
public class HelloBean {

@EJB
HelloService service;

private String testString;

public HelloBean(){
testString = service.sayHello();
}
public String getTestString() {
return testString;
}

public void setTestString(String testString) {
this.testString = testString;
}

}

最佳答案

想一想:您正在 -constructor- 中使用注入(inject)的 bean。但构造函数是第一个被调用的东西,所以当然此时还没有注入(inject)任何东西。

而是在后构造方法中进行。

@ManagedBean
@RequestScoped
public class HelloBean {

@EJB
HelloServiceLocal service;

private String testString;

public HelloBean(){
}

@PostConstruct
public void init(){
testString = service.sayHello();
}

关于java - 无法在 jboss 7 上使用 @EJB 注入(inject) EJB 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868300/

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