具有限定符@Default的类型EntityManager的不满意依赖项

时间:2014-08-09 00:55:17

标签: java maven java-ee jboss cdi

我的项目结构看起来像

project/
  business/pom.xml
  services/pom.xml
  integration/pom.xml

business / pom.xml

    <dependencies>
        <dependency>
            <groupId>com.self</groupId>
            <artifactId>persistence</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

persistence / pom.xml

    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>${hibernate-jpa-2.0-api.version}</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>${querydsl-version}</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>${querydsl-version}</version>
        </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>${querydsl-version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate-entitymanager.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

project / pom.xml

    <dependencies>
        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
            <version>${jsr305.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee-api.version}</version>
        </dependency>
    </dependencies>

business / ClientRegistrationManager.java

@Stateless
public class ClientRegistrationManager {
    private EntityManager entityManager;

    @SuppressWarnings("UnusedDeclaration")
    public ClientRegistrationManager() {
    }

    @Inject
    public ClientRegistrationManager(@Nonnull final EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public ClientDetail register(@Nonnull final String email, final long id, @Nonnull final String password) {
        final UserService userService = new UserService(entityManager);
        final User user = userService.getUserByEmail(email);
        return new ClientDetail("clientId", "clientSecret");
    }
}  

部署时,我看到以下错误

[INFO] [talledLocalContainer] 17:48:53,405 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-15) MSC000001: Failed to start service jboss.deployment.unit."application.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."application.war".WeldStartService: Failed to start service
[INFO] [talledLocalContainer]   at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.0.Final.jar:1.2.0.Final]
[INFO] [talledLocalContainer]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer]   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer]   at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer] Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
[INFO] [talledLocalContainer]   at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.self.oauth.business.oauth.ClientRegistrationManager(@Nonnull EntityManager)
[INFO] [talledLocalContainer]   at com.self.oauth.business.oauth.ClientRegistrationManager.<init>(ClientRegistrationManager.java:0)
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:368)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:289)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:135)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:166)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:514)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
[INFO] [talledLocalContainer]   at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
[INFO] [talledLocalContainer]   at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
[INFO] [talledLocalContainer]   at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
[INFO] [talledLocalContainer]   at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_05]
[INFO] [talledLocalContainer]   ... 3 more
[INFO] [talledLocalContainer] 
[INFO] [talledLocalContainer] 17:48:53,411 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "application.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"application.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"application.war\".WeldStartService: Failed to start service
[INFO] [talledLocalContainer]     Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type EntityManager with qualifiers @Default
[INFO] [talledLocalContainer]   at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public com.self.oauth.business.oauth.ClientRegistrationManager(@Nonnull EntityManager)
[INFO] [talledLocalContainer]   at com.self.oauth.business.oauth.ClientRegistrationManager.<init>(ClientRegistrationManager.java:0)
[INFO] [talledLocalContainer] "}}

我还在以下位置添加了空beans.xml

business/src/main/resources/META-INF/beans.xml
services/src/main/resources/META-INF/beans.xml
services/src/main/webapp/WEB-INF/beans.xml

但他们都没有帮助

你能告诉我我错过了什么吗?

2 个答案:

答案 0 :(得分:5)

我们通常通过&#39; @PersistenceContext(unitname =&#34; name&#34;)&#39;注入EntityManager。为了使用EntityManager的构造函数注入,您可能需要定义一个生成器方法。

@Stateless
public class MyEntityManagerProducer{

  @PersistenceContext(unitname="name")
  private EntityManager entityManager;

  @Produces
  public EntityManager entityManager(){
    return entityManager;
  }

}

然后在构造函数注入中:

  @Inject
    public ClientRegistrationManager(@Nonnull final EntityManager entityManager) {
        this.entityManager = entityManager;
    }

答案 1 :(得分:3)

EntityManager没有默认的制作人方法。您需要创建一个才能使用@Inject