@Autowired @Qualifier("primaryEntityManagerFactory") private EntityManager primaryEntityManager;
@Primary @Bean(name = "primaryEntityManagerFactory") public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) throws NamingException { return builder .dataSource(dataSource) .properties(getVendorProperties(this.primaryDataSource(this.primaryDataSourceProperties()))) .packages("abc") .build(); }
@Qualifier注解的作用是指定name进行注入
此时会找到LocalContainerEntityManagerFactoryBean进行注入,但LocalContainerEntityManagerFactoryBean不是EntityManager,为什么没有报错而且代码正常运行了呢?
请教了下G老师,G老师的回答是:
总结就是LocalContainerEntityManagerFactoryBean被用来创建一个EntityManagerFactory实例,EntityManagerFactory又被用来创建EntityManager实例。 标签:primaryDataSource,EntityManager,Qualifier,EntityManger,JPA,dataSource,LocalConta From: https://www.cnblogs.com/onejay/p/17349301.html