I'm trying to create EAR project with EJB, JPA and Web elements. I have connection to database and think that it works fine. But I am not able to @inject beans from EJB project.
Here's my configuration: In EAR project EarContent/META-INF/application.xml:
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/application_7.xsd" id="Application_ID" version="7">
<display-name>Fantasy</display-name>
<module>
<ejb>FantasyEjb.jar</ejb>
</module>
<module>
<web>
<web-uri>FantasyWeb.war</web-uri>
<context-root>FantasyWeb</context-root>
</web>
</module>
</application>
In EAR project EarContent/WEB-INF/jboss-deployment-structure.xml:
<?xml version="1.0"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="javax.faces.api" />
<module name="com.sun.jsf-impl" />
</exclusions>
<dependencies>
<module name="org.apache.log4j" />
<module name="org.dom4j" />
<module name="org.apache.commons.logging" />
<module name="org.apache.commons.collections" />
<module name="javax.faces.api" />
<module name="com.sun.jsf-impl" />
</dependencies>
</deployment>
</jboss-deployment-structure>
In JPA project file persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="JPADB">
<jta-data-source>java:jboss/datasources/AGHDS</jta-data-source>
<properties>
<property name="showSql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
In EJB project ejbModule/META-INF/ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="3.2" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd">
<display-name>FantasyEjb</display-name>
</ejb-jar>
In WEB project WebContent/WEB-INF/beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
Now I have a bean in EJB project TestBean.java:
package com.fantasy.beans;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.fantasy.db.model.Elfs;
@Stateless
public class TestBean implements Serializable{
private static final long serialVersionUID = 1L;
@PersistenceContext(unitName = "JPADB")
private EntityManager em;
public List<Elfs> query() {
Query query = em.createQuery("FROM com.fantasy.db.model.Elfs");
@SuppressWarnings("unchecked")
List <Elfs> list = query.getResultList();
return list;
}
}
And in WEB project I have class Producer.java which would like to use in JSF page:
package com.fantasy.web;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import com.fantasy.beans.TestBean;
import com.fantasy.db.model.Elfs;
@Named(value = "blogEntryBean")
@RequestScoped
public class Producer implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
TestBean bean;
private List<Elfs> beans;
@Produces
@Named
public List<Elfs> getBeans() {
return beans;
}
public void setBeans(List<Elfs> beans) {
this.beans = beans;
}
@PostConstruct
public void retrieveAllSeatsOrderedByName() {
beans = bean.query();
}
}
The problem is in annoation @inject. Doesn't show errors without it- but I need to synchronize it with my ejb. I think I might missing some dependencies but I don't know how to fix it.
Log:
01:14:02,294 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000227: Running hbm2ddl schema export
01:14:02,390 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 50) HHH000230: Schema export complete
01:14:03,033 SEVERE [javax.enterprise.resource.webcontainer.jsf.flow] (MSC service thread 1-3) Unable to obtain CDI 1.1 utilities for Mojarra
01:14:03,044 SEVERE [javax.enterprise.resource.webcontainer.jsf.application.view] (MSC service thread 1-3) Unable to obtain CDI 1.1 utilities for Moja
rra
01:14:03,173 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-3) MSC000001: Failed to start service jboss.deployment.unit."Fantasy.ear".WeldSt
artService: org.jboss.msc.service.StartException in service jboss.deployment.unit."Fantasy.ear".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_67]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_67]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_67]
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type TestBean with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject com.fantasy.web.Producer.bean
at com.fantasy.web.Producer.bean(Producer.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:372)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:293)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:167)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:531)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
at java.util.concurrent.FutureTask.run(FutureTask.java:262) [rt.jar:1.7.0_67]
... 3 more
01:14:03,183 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("depl
oyment" => "Fantasy.ear")]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"Fantasy.ear\".WeldStartService" => "org
.jboss.msc.service.StartException in service jboss.deployment.unit.\"Fantasy.ear\".WeldStartService: Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type TestBean with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject com.fantasy.web.Producer.bean
at com.fantasy.web.Producer.bean(Producer.java:0)
"}}
01:14:03,253 INFO [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "Fantasy.ear" (runtime-name : "Fantasy.ear")
01:14:03,257 INFO [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
JBAS014777: Services which failed to start: service jboss.deployment.unit."Fantasy.ear".WeldStartService: org.jboss.msc.service.StartException
in service jboss.deployment.unit."Fantasy.ear".WeldStartService: Failed to start service
01:14:03,355 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
01:14:03,356 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
01:14:03,356 ERROR [org.jboss.as] (Controller Boot Thread) JBAS015875: WildFly 8.2.0.Final "Tweek" started (with errors) in 7986ms - Started 385 of 47
3 services (22 services failed or missing dependencies, 108 services are lazy, passive or on-demand)
01:14:03,500 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 51) JBAS011410: Stopping Persistence Unit (phase 2 of 2) Service 'Fantasy.ear#JPAD
B'
01:14:03,501 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 51) HHH000227: Running hbm2ddl schema export
01:14:03,544 INFO [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 51) HHH000230: Schema export complete
01:14:03,546 INFO [org.jboss.weld.deployer] (MSC service thread 1-7) JBAS016009: Stopping weld service for deployment Fantasy.ear
01:14:03,580 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 51) JBAS011410: Stopping Persistence Unit (phase 1 of 2) Service 'Fantasy.ear#JPAD
B'
01:14:03,587 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015974: Stopped subdeployment (runtime-name: FantasyEjb.jar) in 99ms
01:14:03,587 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015974: Stopped subdeployment (runtime-name: FantasyWeb.war) in 99ms
01:14:03,589 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015877: Stopped deployment Fantasy.ear (runtime-name: Fantasy.ear) in
101ms
01:14:03,699 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018558: Undeployed "Fantasy.ear" (runtime-name: "Fantasy.ear")
01:14:03,704 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014775: New missing/unsatisfied dependencies:
service jboss.deployment.subunit."Fantasy.ear"."FantasyEjb.jar".deploymentCompleteService (missing) dependents: [service jboss.deployment.unit."
Fantasy.ear".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."com.sun.faces.config.ConfigureListener".CREATE (missing) dependents:
[service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."com.sun.faces.config.ConfigureListener".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."com.sun.faces.config.ConfigureListener".START (missing) dependents: [
service jboss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService, service jboss.undertow.deployment.default-ser
ver.default-host./FantasyWeb, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."com.sun.faces.config.ConfigureListener".WeldInstantiator (missing) de
pendents: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."com.sun.faces.config.ConfigureListener".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacesServlet".CREATE (missing) dependents: [servic
e jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacesServlet".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacesServlet".START (missing) dependents: [service
jboss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService, service jboss.undertow.deployment.default-server.def
ault-host./FantasyWeb, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacesServlet".WeldInstantiator (missing) dependent
s: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacesServlet".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacetTag".CREATE (missing) dependents: [service jb
oss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacetTag".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacetTag".START (missing) dependents: [service jbo
ss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService, service jboss.undertow.deployment.default-server.default
-host./FantasyWeb, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacetTag".WeldInstantiator (missing) dependents: [
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacetTag".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".CREATE (missing) depe
ndents: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".START (missing) depen
dents: [service jboss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService, service jboss.undertow.deployment.def
ault-server.default-host./FantasyWeb, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".WeldInstantiator (mis
sing) dependents: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".CREATE (missing) dependents
: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".START (missing) dependents:
[service jboss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService, service jboss.undertow.deployment.default-s
erver.default-host./FantasyWeb, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".WeldInstantiator (missing)
dependents: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldInitialListener".CREATE (missing) dependen
ts: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldInitialListener".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldInitialListener".START (missing) dependent
s: [service jboss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService, service jboss.undertow.deployment.default
-server.default-host./FantasyWeb, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldInitialListener".WeldInstantiator (missing
) dependents: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldInitialListener".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldTerminalListener".CREATE (missing) depende
nts: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldTerminalListener".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldTerminalListener".START (missing) dependen
ts: [service jboss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService, service jboss.undertow.deployment.defaul
t-server.default-host./FantasyWeb, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldTerminalListener".WeldInstantiator (missin
g) dependents: [service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldTerminalListener".START]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".deploymentCompleteService (missing) dependents: [service jboss.deployment.unit."
Fantasy.ear".deploymentCompleteService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".ee.ComponentRegistry (missing) dependents: [service jboss.undertow.deployment.de
fault-server.default-host./FantasyWeb.UndertowDeploymentInfoService]
service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".jndiDependencyService (missing) dependents: [service jboss.deployment.subunit."F
antasy.ear"."FantasyWeb.war".component."javax.faces.webapp.FacesServlet".START, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".compon
ent."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".START, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.
servlet.WeldInitialListener".START, service jboss.deployment.subunit."Fantasy.ear"."FantasyWeb.war".component."org.jboss.weld.servlet.WeldTerminalList
ener".START, JBAS014799: ... and 4 more ]
service jboss.deployment.unit."Fantasy.ear".WeldBootstrapService (missing) dependents: [service jboss.deployment.unit."Fantasy.ear".CdiValidator
FactoryService]
service jboss.deployment.unit."Fantasy.ear".WeldStartService (missing) dependents: [service jboss.deployment.unit."Fantasy.ear".CdiValidatorFact
oryService]
service jboss.persistenceunit."Fantasy.ear#JPADB" (missing) dependents: [service jboss.deployment.unit."Fantasy.ear".deploymentCompleteService]
service jboss.undertow.deployment.default-server.default-host./FantasyWeb (missing) dependents: [service jboss.deployment.subunit."Fantasy.ear".
"FantasyWeb.war".deploymentCompleteService]
service jboss.undertow.deployment.default-server.default-host./FantasyWeb.UndertowDeploymentInfoService (missing) dependents: [service jboss.und
ertow.deployment.default-server.default-host./FantasyWeb]
JBAS014777: Services which failed to start: service jboss.deployment.unit."Fantasy.ear".WeldStartService
01:14:08,291 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) JBAS015003: Found Fantasy.ear in deployment directory. To
trigger deployment create a file called Fantasy.ear.dodeploy
In Java EE 6 (on JBoss AS 7.2), I usually worked around this problem by creating producers for the EJBs in a resources class as follows:
This will allow you to
@Inject
them further down the line. There might be more elegant solutions available in Java EE 7, however.