spring-data – Bean名称不明确地匹配相同的bean两次

我正在尝试使用
spring-data-jpa和spring-data-mongodb.在部署时,我的几个存储库部署,直到一个(每次随机一个)无法部署,错误为

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001414:   Bean name is ambiguous. Name    za.co.knonchalant.dead.dao.repositories.UserRepository resolves to beans: 
  - CdiRepositoryBean: type='za.co.knonchalant.dead.dao.repositories.UserRepository', qualifiers=[@javax.enterprise.inject.Default(), @javax.enterprise.inject.Any()],
  - CdiRepositoryBean: type='za.co.knonchalant.dead.dao.repositories.UserRepository', qualifiers=[@javax.enterprise.inject.Default(), @javax.enterprise.inject.Any()]"}}

它似乎是两次匹配相同的界面 – 任何可能发生的事情的想法?

UserRepository接口是

public interface UserRepository extends LocationRepository<User> { }

而LocationRepository是位置的通用根界面,看起来像

@NoRepositoryBean
public interface LocationRepository<T> extends JpaRepository<T, String> {
    List<T> findByPositionWithin(Circle c);

    List<T> findByPositionNear(Point p, Distance distance);

    List<T> findByPositionWithin(Box b);
}

我的配置,我并不过分自信,是

@Configuration
@EnableJpaRepositories
public class CdiConfig {
    @Produces
    @Dependent
    @PersistenceContext(unitName = "mongo-ogm")
    public EntityManager entityManager;

    public
    @Bean
    Mongo mongo() throws Exception {
        return new Mongo("localhost");
    }

    @Produces
    public @Bean MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongo(), "mydatabase");
    }
}

除了以下的persistence.xml之外 – 这是正确的提供者吗?

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="mongo-ogm" transaction-type="JTA">
        <provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
            <property name="hibernate.ogm.datastore.database" value="database"/>
            <property name="hibernate.ogm.datastore.host" value="localhost"/>
            <property name="hibernate.ogm.datastore.provider" value="MONGODB"/>
            <property name="hibernate.ogm.datastore.create_database" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

最佳答案 我想这两个库都有相同的bean,因此您需要在Maven pom.xml中为其中一个库添加提供的范围.看起来Spring正在多个地方找到相同的bean.

点赞