2013-10-19 132 views
0

努力创造和春天有个自定义的库实现和得到以下 错误:春天JPA定制库工作不

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); 
} 

Cu自定厂豆

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; 
    } 
    } 
} 

Spring配置

<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> 

不工作:(任何帮助?

回答

0

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

确保你依然是你,让你的应用程序库扩展GenericRepository,只有那些注入到你的服务,控制器等

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

+0

像这样的评论只会使一个宽松的时间。下次保持自己,尊重别人的时间。谢谢。 – spartancoder

+0

顺便说一句,有一个弹簧数据文件,在“文档”中表达了具体细节,您必须下载提供的源代码示例来注意它。再见。 – spartancoder

+0

像你这样的问题是不可能回答的。请花些时间阅读文档,按照步骤,并寻求帮助,如果失败,这些或某些事情难以理解。随意抛出代码片段,并说:“不起作用”根本不是问题。我没有说:“你做错了,你很愚蠢”。我说:你所展示的代码甚至还没有接近工作,因为你似乎对Spring Data的工作原理缺乏基本的了解,如果这就是你所有的代码。 –