连接数据库Spring引导时出错

时间:2017-09-04 00:56:29

标签: spring jpa spring-boot spring-data-jpa jasypt

Helo我一直试图用Spring Boot连接到数据库一段时间,我一直收到错误:

java.sql.SQLException:url不能为null

我不确定我做错了什么。我试图获得这样的配置的原因是因为我想使用数据库的编码密码。

这就是我的文件:

DataConfig.java

GET <the URL I tried to access> 403 (Forbidden)

EncryptorConfig.java

@Configuration
public class DataConfig {

    @Autowired
    private Environment env;

    @Configuration
    @Profile(value = "dev")
    public class DevPlaceHolderConfig {

        @Bean
        DataSource dataSource() {

            return DataSourceBuilder
                    .create()
                    .username(env.getProperty("spring.datasource.username"))
                    .password(env.getProperty("spring.datasource.password"))
                    .url(env.getProperty("spring.datasource.url"))
                    .driverClassName(env.getProperty("spring.datasource.driver-class-name"))
                    .build();
        }
    }

    @Configuration
    @Profile(value = "prod")
    public class ProdPlaceHolderConfig {
        @Bean
        DataSource dataSource() {

            return DataSourceBuilder
                    .create()
                    .username(env.getProperty("spring.datasource.username"))
                    .password(env.getProperty("spring.datasource.password"))
                    .url(env.getProperty("spring.datasource.url"))
                    .driverClassName(env.getProperty("spring.datasource.driver-class-name"))
                    .build();
        }
    }
}

application.yml

@Configuration
public class EncryptorConfig {

    @Bean
    public EnvironmentStringPBEConfig environmentVariablesConfiguration() {
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPasswordEnvName("APP_ENCRYPTION_PASSWORD");
        return config;
    }

    @Bean
    public PooledPBEStringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(environmentVariablesConfiguration());
        return encryptor;
    }
}

这是堆栈跟踪

spring:
  profiles:
    active: prod
  output:
    ansi:
      enabled: always
---
spring:
  profiles: dev
  application:
    name: App-Dev
  datasource:
    url: jdbc:mysql://localhost:3306/database?useSSL=false
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver 
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect
server:
  port: 8080

---

spring:
  profiles: prod
  application:
    name: App-Prod
  datasource:
    url: jdbc:mysql://localhost:3306/database?useSSL=false
    username: root
    password: ENC(/98CfxmsjKBW5oZsLkLlmw==)
    driver-class-name: com.mysql.jdbc.Driver 
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect
server:
  port: 3000

1 个答案:

答案 0 :(得分:0)

好的,这花了我一段时间,但这就是我解决问题的方法;结果证明是少数。

  1. 确保在配置中包含属性占位符,以便实际属性值可以绑定到。 请务必在此处阅读enter link description here
  2. 现在我的 application.yml 文件如下所示, APP_ENCRYPTION_PASSWORD 是一个包含加密密码的环境变量。

    spring:
      profiles:
        active: prod
      output:
        ansi:
          enabled: detect
    ---
    spring:
      profiles: dev
      application:
        name: App-Dev
      datasource:
        url: jdbc:mysql://localhost:3306/database?useSSL=false
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver 
      jpa:
        show-sql: true
        hibernate:
          ddl-auto: update
        properties:
          hibernate:
            dialect: org.hibernate.dialect.MySQL5Dialect
    server:
      port: 8080
    
    ---
    
    spring:
      profiles: prod
      application:
        name: App-Prod
      datasource:
        url: jdbc:mysql://localhost:3306/database?useSSL=false
        username: root
        password: ENC(/98CfxmsjKBW5oZsLkLlmw==)
        driver-class-name: com.mysql.jdbc.Driver 
      jpa:
        show-sql: true
        hibernate:
          ddl-auto: none
        properties:
          hibernate:
            dialect: org.hibernate.dialect.MySQL5Dialect
    jasypt:
      encryptor:
        algorithm: PBEWithMD5AndDES
        password: ${APP_ENCRYPTION_PASSWORD}
    server:
      port: 3000
    
    1. 只需要一个数据源配置bean。我已将其设置为更改活动配置文件,以便选择正确的配置。
    2. 还创建私有变量并使用@Value()注释绑定它们。还要将@EnableEncryptableProperties添加到类中。

      <强> DataConfig.java

      @Configuration
      @EnableEncryptableProperties
      public class DataConfig {
      
          @Value("${spring.datasource.username}")
          private String userName;
      
          @Value("${spring.datasource.password}")
          private String password;
      
          @Value("${spring.datasource.url}")
          private String url;
      
          @Value("${spring.datasource.driver-class-name}")
          private String driverClassName;
      
      
          @Bean
          DataSource dataSource() {
              return DataSourceBuilder
                      .create()
                      .username(userName)
                      .password(password)
                      .url(url)
                      .driverClassName(driverClassName)
                      .build();
          }
      }
      

      就是它解决了我所有的问题。

相关问题