Моего ApiConfig
:Невозможно @autowired репозиторий. Bean циклическая ссылка
@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?
Как это исправить? Может быть, я могу установить порядок создания бина или что-то еще?
Также: Мне нужно @Autowired repo в классе конфигурации другого модуля.
Пожалуйста, добавьте стек ошибки инициализации Spring к вашему вопросу, чтобы было более ясно, что я буду делать с ошибкой '@Autowired'. –
попробуйте вызвать метод entityManagerFactoryBean.afterPropertiesSet() – shazin
Похоже, вы пытаетесь автосогласовать 'соглашениеPersistence' в класс, отвечающий за настройку JPA. Можете ли вы поделиться более подробной информацией о своих классах '@ Configuration'? –