【问题标题】:spring jpa custom repository not workingspring jpa自定义存储库不起作用
【发布时间】:2013-10-19 16:39:48
【问题描述】:

尝试使用 spring 创建自定义存储库实现并获得以下内容 错误:

caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
  qualifying bean of type [com.mynamespace.domain.repository.GenericRepositoryImpl] 
  found for dependency: expected at least 1 bean which qualifies as autowire 
  candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

这是我的代码(从https://dl.dropboxusercontent.com/u/11115278/src-spring-problem/SpringSimple.zip.renameit下载):

public class GenericRepositoryImpl<T, ID extends Serializable> extends
        SimpleJpaRepository<T, ID> implements GenericRepository<T, ID> {

    private static final long serialVersionUID = 1L;

    private EntityManager entityManager;
    private Class<T> domainClass;

    public GenericRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {

        super(domainClass, entityManager);

        this.entityManager = entityManager;
        this.domainClass = domainClass;
    }

    public List<T> someCustomMethod(Long orgId) {

        return super.findAll();
    }
}

中间接口

@NoRepositoryBean
public interface GenericRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {

  List<T> someCustomMethod(Long orgId);
}

自定义工厂 bean

public class GenericRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
  extends JpaRepositoryFactoryBean<R, T, I> {

  static Logger logger = LoggerFactory.getLogger(GenericRepositoryFactoryBean.class);

  protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {

    logger.debug("#### GenericRepositoryFactoryBean : createRepositoryFactory #################");
    return new GenericRepositoryFactory(entityManager);
  }

  protected static class GenericRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {

    private EntityManager entityManager;

    public GenericRepositoryFactory(EntityManager entityManager) {
      super(entityManager);
      logger.debug("#### GenericRepositoryFactory : createRepositoryFactory #################");
      this.entityManager = entityManager;
    }

    protected Object getTargetRepository(RepositoryMetadata metadata) {
      logger.debug("#### GenericRepositoryFactory : getTargetRepository #################");
       logger.debug("#### GenericRepositoryFactory : getTargetRepository metadata.domainType - %1 entityManager - %2",metadata.getDomainType(),entityManager);
      return new GenericRepositoryImpl<T, I>((Class<T>) metadata.getDomainType(), entityManager);
    }

    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
      logger.debug("#### GenericRepositoryFactory : getRepositoryBaseClass #################");
      // The RepositoryMetadata can be safely ignored, it is used by the JpaRepositoryFactory
      // to check for QueryDslJpaRepository's which is out of scope.
      return GenericRepositoryImpl.class;
    }
  }
}

弹簧配置

<jpa:repositories base-package="com.mynamespace.domain.repository" 
  factory-class="com.mynamespace.domain.factory.GenericRepositoryFactoryBean"/> 

<bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <property name="persistenceUnitName" value="Mypu" />
  <property name="dataSource" >
    <jee:jndi-lookup id="dataSoruce" jndi-name="java:comp/env/jdbc/MyDS" />
  </property>
  <property name="packagesToScan" value="com.mynamespace.domain.model" />
  <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
          <property name="generateDdl" value="false"/>
          <property name="showSql" value="false"/>
          <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect"/>
      </bean>
  </property>
</bean>

<!--TransactionManager-->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="txManager"/>

附加信息:我正在使用 Spring MVC,我有一个 Spring MVC 配置文件和另一个数据文件,这是我的 Spring MVC 文件:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <import resource="classpath:spring/*.xml"/>
    <context:component-scan base-package="com.mynamespace.controllers"/>

    <mvc:annotation-driven />
    <mvc:resources mapping="/css/**" location="/WEB-INF/resources/css/" />
    <mvc:resources mapping="/js/**" location="/WEB-INF/resources/js/" />
    <mvc:resources mapping="/images/**" location="/WEB-INF/resources/images/" />
    <mvc:resources mapping="/font/**" location="/WEB-INF/resources/font/" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>

</beans>

不工作:(有什么帮助吗?

【问题讨论】:

    标签: java hibernate spring-data


    【解决方案1】:

    该异常似乎表明您从应用程序类中某处的注入点引用了GenericRepositoryImpl。不幸的是,这些样本没有显示确切的来源。添加更多原始堆栈跟踪可能有助于确定问题的根本原因。这绝对是一个试图直接自动装配实现类的应用程序组件。

    确保您仍然让您的应用程序存储库扩展 GenericRepository,并且只将它们注入您的服务、控制器等。

    编辑:下面的评论线程源于对提供的示例代码的误解。

    【讨论】:

    • 这样的cmets只会浪费时间。下次留给自己,尊重别人的时间。谢谢。
    • 顺便说一下,有一个 spring-data 文件,其中包含“doc”中表达的确切细节,您必须下载提供的源示例才能注意到它。再见。
    • 像你这样的问题是无法回答的。请花时间阅读文档,按照步骤操作并在失败或难以理解的情况下寻求帮助。随意把代码sn-ps扔在一起,说:“不起作用”根本不是问题。我没有说:“你做错了,你很傻”。我说:你展示的代码甚至还没有接近工作,因为你甚至似乎缺乏对 Spring Data 工作原理的基本了解,如果这就是你所拥有的所有代码。
    • @spartancoder - 实际上,抱歉……我没有注意到第一个代码窗口中的滚动条,所以我认为GenericRepository 界面就是你所拥有的一切:(。检查渲染真的很有帮助当您发布并确保将 Spring 配置与代码混合在一起时。例如,看到下面的配置,我假设您除了这个配置之外没有任何其他配置。我会检查您的资料并相应地更新我的答案。
    • 感谢您的回复。如果您能看一下并帮助我,我将不胜感激。我不想听上去想,也道歉。
    猜你喜欢
    • 2012-06-02
    • 2022-01-25
    • 2018-10-06
    • 2017-11-15
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    相关资源
    最近更新 更多