在Spring Boot中使用PostgreSQL驱动程序创建数据源时出现异常。

14 浏览
0 Comments

在Spring Boot中使用PostgreSQL驱动程序创建数据源时出现异常。

我正在尝试使用Spring Boot创建一个非Web应用程序,参考了MKyong的示例,但是我遇到了以下错误:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)
(...) 其他无关紧要的INFO日志行
2018-12-12 11:45:29.420 ERROR 30866 --- [ main] com.zaxxer.hikari.HikariConfig           : 从HikariConfig类类加载器sun.misc.Launcher$AppClassLoader@18b4aac2中加载驱动程序类org.postgresql.Driver失败
2018-12-12 11:45:29.423  WARN 30866 --- [ main] s.c.a.AnnotationConfigApplicationContext : 运行上下文初始化期间遇到异常- 取消刷新尝试: org.springframework.boot.context.properties.ConfigurationPropertiesBindException: 无法将属性绑定到'LdConfiguration':前缀=datasources.ld,ignoreInvalidFields=false,ignoreUnknownFields=true; 嵌套异常是org.springframework.boot.context.properties.bind.BindException: 无法将属性绑定到'datasources.ld'下的es.ortoplus.LdConfiguration$$EnhancerBySpringCGLIB$$625f0f64
2018-12-12 11:45:29.435  INFO 30866 --- [ main] ConditionEvaluationReportLoggingListener : 
启动应用程序上下文时出错。要显示条件报告,请使用'debug'参数重新运行应用程序。
2018-12-12 11:45:29.440 ERROR 30866 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
应用程序启动失败
***************************
描述:
无法将属性绑定到'datasources.ld'下的es.oplus.LdConfiguration$$EnhancerBySpringCGLIB$$625f0f64:
    属性:datasources.ld.driverClassName
    值:org.postgresql.Driver
    来源:类路径资源[application.yml]:3:22
    原因:无法从HikariConfig类加载器或线程上下文类加载器中加载驱动程序类org.postgresql.Driver
操作:
更新应用程序的配置

我的配置文件(application.yml)如下:

datasources:

ld:

driverClassName: org.postgresql.Driver

jdbc-url: jdbc:postgresql://localhost:5432/oplus

username: user123

password: 123456

connection-test-query: SELECT 1

在我的Maven pom.xml文件中,我添加了以下内容:


  org.postgresql
  postgresql
  

我的入口类:

@SpringBootApplication
public class App implements CommandLineRunner {
    @Autowired private UsuarioRepository usuarioRep;
    @Override
    public void run(String... args) throws Exception {
        App app = new App();
        System.out.printf("Users: %1d", app.usuarioRep.count());
    }
    public static void main(String[] args) throws ClassNotFoundException {
        //Class.forName("org.postgresql.Driver");
        SpringApplication.run(App.class, args);
    }
}

如您所见,我尝试检查类是否已经在类路径中。如果我取消注释那行代码,我会得到一个ClassNotFoundException,所以似乎错误是由于Maven没有包含该依赖项引起的。我尝试将范围设置为runtime,但仍然失败。

无论如何,这是我的配置类:

@Configuration
@ConfigurationProperties(prefix = "datasources.ld")
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "postgreEntityManagerFactory", transactionManagerRef = "postgreTransactionManager",
        basePackages = "es.plus.l.dao")
public class LdConfiguration extends HikariConfig {
    @Bean(name = "postgreDataSource")
    @Primary
    public DataSource dataSource() {
        return new HikariDataSource(this);
    }
    @Bean(name = "postgreEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean postgreEntityManagerFactory(
            final EntityManagerFactoryBuilder builder,
            @Qualifier("postgreDataSource") final DataSource dataSource) {
        final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setJpaVendorAdapter(this.vendorAdaptor());
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPersistenceUnitName("postgre");
        entityManagerFactoryBean.setPackagesToScan("es.oplus.ld.model");
        entityManagerFactoryBean.setJpaProperties(this.jpaHibernateProperties());
        entityManagerFactoryBean.afterPropertiesSet();
        return entityManagerFactoryBean;
    }
    @Bean(name = "postgreTransactionManager")
    @Primary
    public PlatformTransactionManager postgreTransactionManager(
          @Qualifier("postgreEntityManagerFactory") final EntityManagerFactory emf) {
        return new JpaTransactionManager(emf);
    }
    private HibernateJpaVendorAdapter vendorAdaptor() {
        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        // put all the adapter properties here, such as show sql
        return vendorAdapter;
    }
    private Properties jpaHibernateProperties() {
        final Properties properties = new Properties();
        // put all required jpa propeties here
        return properties;
    }
}

0