In a Spring Boot application I'm trying to setup multiple database connections. I've started building the primary datasource, but I'm getting the following error on the mySqlEntityManagerFactory method.
Could not autowire. no beans of EntityManagerFactoryBuilder
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; import javax.persistence.PersistenceContext; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; @Configuration @Transactional @EnableTransactionManagement @EnableJpaRepositories( basePackages = "digital.sheppard.dao", entityManagerFactoryRef = "entityManager", transactionManagerRef = "transactionManager") public class PrimaryDBConfig { @Bean(name="dataSource") @Primary @ConfigurationProperties(prefix = "primary.datasource.mysql") public DataSource mysqlDataSource() { return DataSourceBuilder.create().build(); } @PersistenceContext(unitName = "primary") @Primary @Bean(name = "entityManager") public LocalContainerEntityManagerFactoryBean mySqlEntityManagerFactory(EntityManagerFactoryBuilder builder) { return builder.dataSource(mysqlDataSource()).persistenceUnit("primary").properties(jpaProperties()) .packages("digital.sheppard.model").build(); } private Map<String, Object> jpaProperties() { Map<String, Object> props = new HashMap<String, Object>(); props.put("hibernte.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy"); props.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); return props; } }
How would I autowire the EntityManagerFactoryBuilder?
I'm trying to follow the code on this blog https://raymondhlee.wordpress.com/2015/10/31/configuring-multiple-jpa-entity-managers-in-spring-boot/
Here's the main application class if it's helpful
@Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) @ComponentScan public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
I think you should remove this code
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With