我正在尝试通过自动装配环境变量来检索从.yml文件加载的属性,但出现空指针异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/example/AppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.NullPointerException
我想以编程方式创建一个DataSource bean,同时将详细信息(用户名,密码,主机等)保留在配置文件中。这是我目前的设置:
@SpringBootApplication
@ImportResource({"classpath:controllers.xml"})
public class WebApplication{
public static void main(String[] args){
SpringApplication.run(WebApplication.class, args);
}
}
server:
port: 8080
database:
host: localhost
instance: db_instance
port: 3036
user: root
password: passkey
@Configuration
public class AppConfig {
@Autowired
private Environment environment;
@Bean
public DataSource dataSource() {
String url = "jdbc:mysql://" +
environment.getProperty("database.host") +
":" + environment.getProperty("database.port") +
"/" + environment.getProperty("database.instance") +
"?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true";
return DataSourceBuilder.create()
.driverClassName("com.mysql.jdbc.Driver")
.url(url)
.username(environment.getProperty("database.user"))
.password(environment.getProperty("database.password"))
.build();
}
}
我既不喜欢.yml文件格式,也不喜欢使用Environment变量。如果要从.yml文件(或某些其他文件格式)中获取数据,有其他更好的方法,我愿意尝试。
答案 0 :(得分:2)
有很多方法可以做到这一点。
由于使用的是Springboot,因此不必创建一个
数据源明确地是这样的(如果这样做,则会丢失springboot的主要功能之一)。如果您在中声明参数
带有右键的properties / yml,带有自动配置功能的Springboot
给你的
搜索spring.datasource......
here
如果您希望自己执行此操作,则可以使用以下命令将properties / yml文件中的所有变量自动连接到Bean中:
ConfigurationProperties
,然后将此bean用作方法参数
您的bean创建方法。
签出this
@Value{}
,并在数据源创建方法中使用它。答案 1 :(得分:0)
不太确定controllers.xml中有什么。 但是我能够获取并使用环境变量。
我将您在application.yml中提到的属性放在/ src / main / resources
中我使用Spring boot-2.1.9版本使其工作,下面是我的pom.xml-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>stackoverflow</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>stackoverflow</name>
<description>stackoverflow</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>ojo-snapshots</id>
<name>OJO Snapshots</name>
<url>https://oss.jfrog.org/artifactory/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>