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
答案 0 :(得分:0)
好的,这花了我一段时间,但这就是我解决问题的方法;结果证明是少数。
现在我的 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
还创建私有变量并使用@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();
}
}
就是它解决了我所有的问题。