Нет подходящего компонента типа 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder - PullRequest
3 голосов
/ 04 февраля 2020

Прежде всего, это не повторяющаяся проблема. Поскольку я настроил правильно, посмотрите мой код и скажите, что здесь не так. Затем здесь я пытаюсь настроить конфигурацию источника данных Spring Boot.

Ошибка, с которой я столкнулся:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'workerEntityManagerFactory' defined in class path resource [com/sample/engine/config/WorkerDBConfiguration.class]: Unsatisfied dependency expressed through method 'workerEntityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:539)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:152)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:132)
        at org.springframework.boot.web.servlet.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:92)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5135)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        ... 38 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:885)
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:789)

Мой класс конфигурации

Первичный Конфигурация данных:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "customerApiEntityManagerFactory",
        transactionManagerRef = "customerTransactionManager",
        basePackages = {
            "com.sample.engine.agent.customerdb.dao"
        }
)
public class CustomerDBConfiguration {

    @Value("${customer.datasource.jndi-name}")
    private String customerDB;

    @Bean(name = "customerDataSource")
    @Primary
    public DataSource customerDataSource() {
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource(customerDB);
        return dataSource;
    }

    @Primary
    @Bean(name = "customerEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder
    ) {
        return builder
                .dataSource(customerDataSource())
                .packages("com.sample.engine.agent.customer.model")
                .persistenceUnit("db1")
                .build();
    }

    @Primary
    @Bean(name = "customerTransactionManager")
    public PlatformTransactionManager customerTransactionManager(@Qualifier("customerEntityManagerFactory") EntityManagerFactory customerEntityManagerFactory) {
        return new JpaTransactionManager(customerEntityManagerFactory);
    }

}

Конфигурация вторичных данных

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "workerEntityManagerFactory",
        transactionManagerRef = "workerTransactionManager",
        basePackages = {
            "com.sample.engine.agent.worker.dao"
        }
)
public class WorkerDBConfiguration {

    @Value("${worker.datasource.jndi-name}")
    private String workerDB;

    @Bean(name = "workerDataSource")
    public DataSource workerDataSource() {
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource(workerDB);
        return dataSource;
    }

    @Bean(name = "workerEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean
            workerEntityManagerFactory(EntityManagerFactoryBuilder workerBuilder) {//, @Qualifier("workerDataSource") DataSource dataSource
        return workerBuilder
                .dataSource(ceapiDataSource())
                .packages("com.sample.engine.agent.worker.model")
                .persistenceUnit("db2")
                .build();
    }

    @Bean(name = "workerTransactionManager")
    public PlatformTransactionManager workerTransactionManager(@Qualifier("workerEntityManagerFactory") EntityManagerFactoryworkerEntityManagerFactory) {
        return new JpaTransactionManager(workerEntityManagerFactory);
    }
}

Основной класс

@SpringBootApplication
@EnableMongoRepositories
@ComponentScan
@EnableAutoConfiguration(exclude = {HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})
public class AgentManagerWeb extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(AgentManagerWeb.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AgentManagerWeb.class);
    }
}

Приложение. Свойства

saasapi.datasource.jndi-name=java:comp/env/jdbc/saasapi
ceapi.datasource.jndi-name=java:comp/env/jdbc/ceapi
...