无法使用Spring云配置的@RefreshScope注入DataSource

时间:2018-02-14 08:50:59

标签: java spring spring-boot spring-data-jpa spring-cloud

我正在尝试使用RefreshScope连接到mysql以获得相同的要求,并且它会抛出以下错误

Could not obtain connection to query metadata : Error creating bean with name 'scopedTarget.userDataSource' defined in class path resource [com/concretepage/UserConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'userDataSource' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [default driver]
2018-02-14 02:24:05.844  WARN 8920 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userEntityManager' defined in class path resource [com/concretepage/UserConfig.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
2018-02-14 02:24:05.846  INFO 8920 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-02-14 02:24:05.859  WARN 8920 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
com.mysql.cj.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
2018-02-14 02:24:05.890  INFO 8920 --- [           main] utoConfigurationReportLoggingInitializer : 

下面是我在userDataSource bean中初始化属性的代码

@Configuration
//@PropertySource({ "classpath:application.properties" })
@EnableJpaRepositories(basePackages = "com.concretepage", entityManagerFactoryRef = "userEntityManager", transactionManagerRef = "userTransactionManager")

public class UserConfig {

    @Value("${jdbc.driverClassName: default driver}")
    private String driver;

// Properties
    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean userEntityManager() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(userDataSource());
        em.setPackagesToScan(new String[] { "com.concretepage" });

        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", hbm2ddl);
        properties.put("hibernate.dialect", dialect);
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Primary
    @Bean
    @RefreshScope
    public DataSource userDataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(usename);
        dataSource.setPassword(password);

        return dataSource;
    }

    @Primary
    @Bean
    public PlatformTransactionManager userTransactionManager() {

        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(userEntityManager().getObject());
        return transactionManager;
    }
}

此代码不使用RefreshScope。我的配置有什么问题吗?

0 个答案:

没有答案