的同事,我'嘗試使用Spring庫界面污物從數據庫獲取數據:如何修復從Spring Data Jpa使用CrudRepository時的NoSuchBeanDefinitionException?
@Component
public interface AssetsGapSortDAO extends CrudRepository<AssetsGapSortEntity, Integer> {
AssetsGapSortEntity getByIsin(String ISIN);
}
JPA實體的樣子:
@Entity
@Table(name = "Assets", schema = "dbo", catalog = "")
public class AssetsGapSortEntity {
private BigInteger id;
private String shortName;
private String isin;
private int registrationDate;
/*Some other properties*/
@Basic
@Column(name = "id")
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
@Id
@Column(name = "ShortName")
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
@Basic
@Column(name = "ISIN")
public String getIsin() {
return isin;
}
public void setIsin(String isin) {
this.isin = isin;
}
/*Equals and hashcode methods*/
@Override
public String toString() {
ToStringStyle style = new NotNullToStringStyle();
return ToStringBuilder.reflectionToString(this, style);
}
}
當我運行JUnit測試:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = OlmeSmbConfig.class, loader = AnnotationConfigContextLoader.class)
public class AssetsGapSortDAOTest {
private static final Logger log = LoggerFactory.getLogger(ExtractionsDAOTest.class);
@Autowired
AssetsGapSortDAO assetsGapSortDAO;
@Test
public void getByIsin() throws Exception {
log.info(assetsGapSortDAO.getByIsin("TT0W3888254").toString());
}
}
我收到naxt stackTrace:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
....
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'assetInfoLoader': Unsatisfied dependency expressed through field 'assetsGapSortDAO'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.comp.olme.jpa.dao.GapSort.AssetsGapSortDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
....
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 29 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.comp.olme.jpa.dao.GapSort.AssetsGapSortDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 45 more
我的配置是這樣的:
@Configuration
@EnableTransactionManagement
@ComponentScan (basePackages = {"com.comp.olme"})
@PropertySource("classpath:OlmeSmb-${env}.properties")
@EnableScheduling
public class OlmeSmbConfig {
/*There are DataSource, EntityManagerFactory beans etc....*/
}
當我用簡單的JPA一切工作正常,但我想嘗試CrudRepository。 所以問題是爲什麼我收到NoSuchBeanDefinitionException
以及如何解決它? 謝謝。
UPDATE
根據@javaguy建議我加入到我的配置@EnableJpaRepositories
註釋:
@EnableJpaRepositories(basePackages="com.comp.olme", entityManagerFactoryRef ="emGapSort", transactionManagerRef = "txManagerGapSort")
和 「NoSuchBeanDefinitionException」 是固定的,但新的提高:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#3e4e8fdf': Unsatisfied dependency expressed through method 'createSharedEntityManager' parameter 0:
Could not convert argument value of type [com.sun.proxy.$Proxy44] to required type [javax.persistence.EntityManagerFactory]:
Failed to convert value of type 'com.sun.proxy.$Proxy44 implementing org.hibernate.jpa.HibernateEntityManager,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManagerFactory';
nested exception is java.lang.IllegalStateException:
Cannot convert value of type 'com.sun.proxy.$Proxy44 implementing org.hibernate.jpa.HibernateEntityManager,org.springframework.orm.jpa.EntityManagerProxy' to required type 'javax.persistence.EntityManagerFactory':
no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:723)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:467)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1028)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
... 58 more
可能是因爲我在Spring Config中有多個數據源(有兩個用於diffe租DB)?如何解決它? 很抱歉,如果它的範圍...
爲什麼用'@ Component'註釋'AssetsGapSortDAO'?我不認爲這是必要的 – Pons