Collegues, I'am essayant d'obtenir des données de base de données à l'aide du référentiel Spring Crud:Comment réparer NoSuchBeanDefinitionException en utilisant CrudRepository de Spring Data Jpa?
@Component
public interface AssetsGapSortDAO extends CrudRepository<AssetsGapSortEntity, Integer> {
AssetsGapSortEntity getByIsin(String ISIN);
}
entité JPA ressemble à:
@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);
}
}
Quand je lance test 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());
}
}
je reçois la pile naxtTrace:
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
Ma config ressemble:
@Configuration
@EnableTransactionManagement
@ComponentScan (basePackages = {"com.comp.olme"})
@PropertySource("classpath:OlmeSmb-${env}.properties")
@EnableScheduling
public class OlmeSmbConfig {
/*There are DataSource, EntityManagerFactory beans etc....*/
}
Quand j'utilise tout simplement JPA fonctionne très bien, mais je voudrais essayer CrudRepository. Donc la question est pourquoi je reçois NoSuchBeanDefinitionException
et comment y remédier? Merci.
MISE À JOUR
Selon @javaguy conseils, j'ai ajouté à mon annotation config @EnableJpaRepositories
:
@EnableJpaRepositories(basePackages="com.comp.olme", entityManagerFactoryRef ="emGapSort", transactionManagerRef = "txManagerGapSort")
et le "NoSuchBeanDefinitionException" a été fixé, mais la nouvelle augmentation:
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
Peut-être parce que j'ai plus d'une source de données dans Spring Config (il y en a deux pour diffe louer DB)? Comment le réparer? Désolé si elle hors de portée ...
Pourquoi annotez-vous 'AssetsGapSortDAO' avec' @ Component'? Je ne pense pas qu'il soit nécessaire – Pons