gpt4 book ai didi

java - 如何在 BaseServiceImpl 中 Autowiring @NoRepositoryBean BaseRepository

转载 作者:搜寻专家 更新时间:2023-10-31 20:32:32 24 4
gpt4 key购买 nike

基础库

@NoRepositoryBean
public interface BaseRepository<T extends BaseEntity, ID extends Serializable>
extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

T findByIdAndDeleteStatusFalse(ID id);
}

BaseServiceImpl

@Transactional(readOnly = true)
public abstract class BaseServiceImpl<T extends BaseEntity, ID extends Serializable> implements BaseService<T, ID> {

@Autowired
protected BaseRepository<T, ID> baseRepository;
}

applicationContext.xml中的jpa配置

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.coderbike.entity, com.coderbike.core.entity"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="showSql" value="false"/>
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<property name="database" value="MYSQL"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>

<!-- annotation transaction -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

<!-- scan repository package -->
<jpa:repositories base-package="com.coderbike.dao.jpa, com.coderbike.core.repository"
repository-impl-postfix="Impl"
transaction-manager-ref="transactionManager"
entity-manager-factory-ref="entityManagerFactory" />

启动tomcat时出现错误

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.coderbike.core.repository.BaseRepository] found for dependency [com.coderbike.core.repository.BaseRepository<com.coderbike.entity.User, java.lang.Integer>]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
... 60 more

我在网上找了很久。但最流行的场景是创建一个 BaseRepositoryImpl 并自定义扩展 JpaRepositoryFactory 的 BaseRepositoryFactory。那么我可以使用 Autowiring 来代替上面的场景吗?

最佳答案

@NoRepositoryBean 的概念不是为您创建特定存储库接口(interface)的代理实例。有很好的解释是here .

这意味着您应该扩展此 BaseRepository 接口(interface)并提供和实现您自己的接口(interface)。一旦完成,然后在您的 xml 中声明该 bean。

关于java - 如何在 BaseServiceImpl 中 Autowiring @NoRepositoryBean BaseRepository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40299041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com