Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot EntityManagerFactoryBuilder not autowired

Tags:

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);     }  } 
like image 283
SheppardDigital Avatar asked Nov 21 '15 18:11

SheppardDigital


Video Answer


1 Answers

I think you should remove this code

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 
like image 127
Y.Ido Avatar answered Sep 17 '22 09:09

Y.Ido