我花了一天时间试图解决这个问题,并最终不得不转向stackoverflow以获得专家建议。
我使用java设置spring配置,有两个数据源和两个LocalContainerEntityManagerFactoryBean,如下所示。但默认情况下,Spring似乎寻找entityManagerFactory()并抛出异常(包括下面的stacktrace)。如何配置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'entalManagerFactory'的引用;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'entityManagerFactory'的bean 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) 在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.instantiateUsingFactoryMethod(ConstructorResolver.java:441) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1015) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 在org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271) ... 51更多
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'entityManagerFactory'的bean at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:549) 在org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) 在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;
}
}
答案 0 :(得分:1)
一种可能的解决方案。
public class MyRepositoryImpl extends JdbcDaoSupport implements MyRepository {
@Qualifier("odsDataSource")
@Autowired(required = true)
private DataSource ds;
public MyRepositoryImpl() {
}
@PostConstruct
void postConstruct() {
setDataSource(ds);
}
...
}
如果通过扩展JdbcDaoSupport(以及其他一些Spring提供的帮助程序)来开发存储库,那么它们将默认尝试在默认的“entityManagerFactory”上按名称连接。因此,您需要通过注入备用命名的数据bean来初始化它们。
假设您使用JPA的另一种可能性。
@Repository
public class MyRepositoryImpl implements MyRepository {
@PersistenceContext(unitName="MyPersistenceUnit")
private EntityManager entityManager;
public List<MyModel> findAll() {
return entityManager.createNamedQuery("MyModel.findAll").getResultList();
}
}
更新:对于最近遇到此问题的人,使用Spring Boot和Spring Data JPA,我写了一篇博文,其中介绍了实现这项工作的基本要点:http://scattercode.co.uk/2016/01/05/multiple-databases-with-spring-boot-and-spring-data-jpa/