【问题标题】:CDI error: Ambiguous dependencies trying to inject EntityManagerCDI 错误:模糊的依赖项试图注入 EntityManager
【发布时间】:2015-05-08 13:33:02
【问题描述】:

尝试在 JBoss 7.1.1 应用服务器中部署 EJB 3.1 应用程序时遇到一个奇怪的错误:WELD-001409 Ambiguous dependencies for type [EntityManager] with qualifiers [@Default] at injection point [[parameter 1] of [constructor] @Inject public br.com.sigga.siot.dao.masterdata.impl.ProfileDAOImpl(EntityManager)]. Possible dependencies [[Resource Producer Field [EntityManager] with qualifiers [@Any @Default] declared as [[field] @PersistenceContext @Produces private br.com.sigga.siot.cdi.JPAProducer.entityManager], Resource Producer Field [EntityManager] with qualifiers [@Any @Default] declared as [[field] @PersistenceContext @Produces private br.com.sigga.siot.cdi.JPAProducer.entityManager]]]

如您所见,“可能的依赖关系”两次指向同一个 EntityManager 生产者:br.com.sigga.siot.cdi.JPAProducer.entityManager。该字段声明如下:

@PersistenceContext(unitName = "siotMobility")
@Produces
private EntityManager entityManager;

WELD-001409 Ambiguous dependencies 中有一个类似案例,但我无法将我的应用程序服务器更改为 Glassfish 4.1。 :-)

我的开发环境:Eclipse Luna、Java 7、Maven 3.x(eclipse 的嵌入式版本)。我有一个带有 3 个模块(siot-mobility-[ear|ejb|web])的 Maven 项目(siot-mobility)。

EAR 模块的 POM 声明了以下依赖项:

<dependencies>
    <dependency>
        <groupId>br.com.sigga</groupId>
        <artifactId>siot-mobility-web</artifactId>
        <version>${project.version}</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>br.com.sigga</groupId>
        <artifactId>siot-mobility-ejb</artifactId>
        <version>${project.version}</version>
        <type>ejb</type>
    </dependency>
</dependencies>

Maven EAR 插件配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-ear-plugin</artifactId>
    <configuration>
        <version>6</version>
        <defaultLibBundleDir>lib</defaultLibBundleDir>
        <modules>
            <webModule>
                <groupId>br.com.sigga</groupId>
                <artifactId>siot-mobility-web</artifactId>
            </webModule>
            <ejbModule>
                <groupId>br.com.sigga</groupId>
                <artifactId>siot-mobility-ejb</artifactId>
            </ejbModule>
        </modules>
    </configuration>
</plugin>

请随时向我询问可以帮助您的更多信息。 :-) 提前致谢。

【问题讨论】:

    标签: jboss7.x cdi entitymanager jboss-weld


    【解决方案1】:

    发现错误:EJB 模块在生成的 EAR 中出现了两次,一次出现在 EAR 文件的根路径中,第二次出现在 WAR 文件的 WEB-INF/lib 文件夹中。

    我将 WAR 项目的 POM 文件中的依赖声明更改为:

    <dependency>
        <groupId>br.com.sigga</groupId>
        <artifactId>siot-mobility-ejb</artifactId>
    </dependency>
    

    到:

    <dependency>
        <groupId>br.com.sigga</groupId>
        <artifactId>siot-mobility-ejb</artifactId>
        <scope>provided</scope>
    </dependency>
    

    这样,EJB 模块不会被复制到 WAR 文件的 WEB-INF/lib 文件夹中,并且我在生成的 EAR 文件中只有一个 EntityManager 生产者。

    【讨论】:

      猜你喜欢
      • 2012-04-28
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2021-09-08
      • 1970-01-01
      • 2016-06-07
      • 2019-05-26
      相关资源
      最近更新 更多