我ApiConfig
:不能@autowired存儲庫。豆循環引用
@Configuration
@EnableConfigurationProperties(JpaProperties.class)
@EnableJpaRepositories("com.foo.api.persistence")
@ComponentScan("com.foo.api")
@PropertySource("classpath:application.yaml")
public class ApiConfig {
private static final Logger LOGGER = LogManager.getLogger();
private static final String MODEL_PACKAGE = "com.foo.api.model";
@Autowired
private MultiTenantConnectionProviderImpl multiTenantConnectionProvider;
@Autowired
private ApplicationContext applicationContext;
@Resource
private org.springframework.core.env.Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws CurrentTenantIdentifierResolverException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setPackagesToScan(MODEL_PACKAGE);
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
Properties jpaProperties = new Properties();
jpaProperties.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
jpaProperties.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
jpaProperties.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, getCurrentTenantIdentifierResolver());
jpaProperties.put(Environment.DIALECT, getHibernateDialect());
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
private CurrentTenantIdentifierResolver getCurrentTenantIdentifierResolver() throws CurrentTenantIdentifierResolverException {
CurrentTenantIdentifierResolver resolver = applicationContext.getBean(CurrentTenantIdentifierResolver.class);
if (resolver == null) {
throw new CurrentTenantIdentifierResolverException();
}
return resolver;
}
private String getHibernateDialect() {
return env.getProperty(PropertyConstants.DIALECT);
}
}
但現在我無法@Autowired
我JpaRepository
:
@Autowired
private AgreementPersistence agreementPersistence;
協議的持久性是簡單的回購從JpaRepository擴展了一個自己的基於查詢的方法;
異常日誌:
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: AgreementPersistence agreementPersistence; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'agreementPersistence': Cannot create inner bean '(inner bean)#38830ea' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '(inner bean)#38830ea': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
如何解決呢? 也許我可以設置一些bean創建順序或其他東西?
另外: 我需要@Autowired回購在另一個模塊的配置類。
請將Spring初始化錯誤堆棧添加到您的問題中,以便更清楚「@Autowired」注入會出錯。 –
嘗試調用entityManagerFactoryBean.afterPropertiesSet()方法 – shazin
它看起來像你試圖將'AgreementPersistence'自動裝入一個負責配置JPA的類。你能分享你的'@ Configuration'類的更多細節嗎? –