我花了一天时间试图弄清楚,最终不得不求助于 stackoverflow 寻求一些专家建议。
我正在使用 java 设置 spring 配置以具有两个数据源和两个 LocalContainerEntityManagerFactoryBean,如下所示。但默认情况下,Spring 似乎寻找 entityManagerFactory() 并抛出异常(包括下面的堆栈跟踪)。如何配置 spring 以使用我的 entitymanagerfactories 而不是默认名称。
@Bean(name="visionDataSource")
public DataSource visionDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// set properties..
return dataSource;
}
@Bean(name="stormDataSource")
public DataSource visionDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
// set properties..
return dataSource;
}
@Bean(name="visionentityManagerFactory")
public LocalContainerEntityManagerFactoryBean visionentityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(visionDataSource());
//entityManagerFactoryBean.setPersistenceUnitManager(persistenceUnitManager());
//entityManagerFactoryBean.setPersistenceXmlLocation(null);
entityManagerFactoryBean.setPersistenceUnitName("visionEntityManagerFactory");
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistence.class);
entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
entityManagerFactoryBean.setJpaProperties(hibProperties());
entityManagerFactoryBean.afterPropertiesSet();
System.out.println("*********************visionEntityManagerFactory********************");
return entityManagerFactoryBean;
}
@Bean(name="stormEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean stormEntityManagerFactory() {
entityManagerFactoryBean.setDataSource(stormDataSource());
// set other properties
return entityManagerFactoryBean;
}
错误日志:
引起:org.springframework.beans.factory.BeanCreationException:创建名称为“(内部bean)#1”的bean时出错:设置构造函数参数时无法解析对bean“entityManagerFactory”的引用;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) at org.springframework.beans.factory .support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) 在 org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:616) 在 org.springframework.beans.factory.support.ConstructorResolver。
原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549) 在 org.springframework.beans.factory 中没有定义名为“entityManagerFactory”的 bean .support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory .java:193) 在 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323) ... 59 更多
更新:我正在使用 JPARepository
public interface ContactRepository extends JpaRepository<Contact, Serializable> {
List<Contact> findByCompanyId(int companyId);
}
我已经尝试扩展自定义存储库注入实体管理器,如下所示。但这也无济于事。
public interface ContactRepository extends DefaultRepository<Contact, Serializable> {
List<Contact> findByCompanyId(int companyId);
}
@NoRepositoryBean
public class DefaultRepositoryImpl<T, ID extends Serializable> extends SmpleJpaRepository<T, ID> implements
DefaultRepository<T,ID> {
@PersistenceContext(unitName="visionEntityManagerFactory")
private EntityManager entityManager;
/**
* @param domainClass
* @param em
*/
public DefaultRepositoryImpl(Class<T> domainClass, EntityManager em) {
super(domainClass, em);
}
/**
* {@inheritDoc}
*/
protected Object getTargetRepository(RepositoryMetadata metadata) {
return new DefaultRepositoryImpl<T, ID>((Class<T>) metadata.getDomainType(), entityManager);
}
/**
* {@inheritDoc}
*/
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return DefaultRepository.class;
}
}