执行@Bean注解方法时自动装配环境变量为NULL

我试图通过自动装配环境变量来检索从 .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 文件格式或使用环境变量。如果有不同/更好的方法来从 .yml 文件(或其他文件格式)获取数据,我愿意尝试。


汪汪一只猫
浏览 140回答 2
2回答

翻阅古今

有很多方法可以做到这一点。由于您使用的是 Springboot,因此不必像这样显式创建数据源(如果这样做,您将错过 springboot 的主要功能之一)。如果你用右键在properties/yml中声明参数,Springboot会自动为你配置这个。搜索spring.datasource......如果您希望自己执行此操作,那么您可以使用以下命令将properties/yml文件中的所有变量自动装配到Bean中, ConfigurationProperties然后在bean创建方法中使用该bean作为方法参数。在您的 AppConfig 类中使用@Value{}并在您的数据源创建方法中使用它。

慕码人8056858

不太确定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"&nbsp; &nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <groupId>com.sample</groupId>&nbsp; &nbsp; <artifactId>stackoverflow</artifactId>&nbsp; &nbsp; <version>0.0.1</version>&nbsp; &nbsp; <packaging>jar</packaging>&nbsp; &nbsp; <name>stackoverflow</name>&nbsp; &nbsp; <description>stackoverflow</description>&nbsp; &nbsp; <parent>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.1.9.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; <relativePath /> <!-- lookup parent from repository -->&nbsp; &nbsp; </parent>&nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>&nbsp; &nbsp; &nbsp; &nbsp; <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>&nbsp; &nbsp; &nbsp; &nbsp; <java.version>1.8</java.version>&nbsp; &nbsp; </properties>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-data-jpa</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>mysql</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>mysql-connector-java</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </build>&nbsp; &nbsp; <repositories>&nbsp; &nbsp; &nbsp; &nbsp; <repository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>spring-snapshots</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>Spring Snapshots</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>https://repo.spring.io/libs-snapshot-local</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <snapshots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <enabled>true</enabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </snapshots>&nbsp; &nbsp; &nbsp; &nbsp; </repository>&nbsp; &nbsp; &nbsp; &nbsp; <repository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>ojo-snapshots</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>OJO Snapshots</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>https://oss.jfrog.org/artifactory/libs-snapshot</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <snapshots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <enabled>true</enabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </snapshots>&nbsp; &nbsp; &nbsp; &nbsp; </repository>&nbsp; &nbsp; </repositories></project>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java