为什么从属性文件读取时环境变量始终为空?

时间:2021-03-02 20:49:24

标签: java spring-boot

我刚开始使用 application.properties 并且很挣扎,因为 Environment 始终为空。我遵循了许多示例,例如 SO HERE 上显示的已接受答案;但是,我不断收到下面显示的错误消息。这是一个 Spring 应用程序,我知道我实际上不需要按照文章 HERE 执行此手动过程。但是,我必须展示如何使用属性文件手动连接。我哪里错了?实际上,我想在 application.properties 中隐藏我的数据库凭据,然后将它们读入类似 DriverManager.getConnection(env.getProperty("URL"), env.getProperty("USERNAME", env.getProperty("PASSWORD");

尝试使用环境:

@Component
@PropertySource("classpath:application.properties")
public class OpenConnection {
    private volatile Connection con;
    @Autowired
    private Environment env;


    public OpenConnection() {
    }

    @Bean
    public Connection getInstance() {
        try {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
            dataSource.setUrl(env.getProperty("spring.datasource.url"));
            dataSource.setUsername(env.getProperty("spring.datasource.username"));
            dataSource.setPassword(env.getProperty("spring.datasource.password"));
            con = dataSource.getConnection();
        } catch (SQLException e) {
            handleException(e);
        }

        return con;
    }

application.properties

# MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rentalportfolio?useTimezone=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

错误信息

java.lang.NullPointerException: null
    at com.crd.carrental.database.connectionoperations.OpenConnection.getInstance(OpenConnection.java:43) ~[classes/:na]
    at com.crd.carrental.database.selectoperations.SelectExistingReservation.<init>(SelectExistingReservation.java:24) ~[classes/:na]
    at com.crd.carrental.controllers.ExistingReservationController.lookupReservationId(ExistingReservationController.java:24) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_261]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_261]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_261]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_261]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:171) ~[spring-messaging-5.3.3.jar:5.3.3]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120) ~[spring-messaging-5.3.3.jar:5.3.3]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMatch(AbstractMethodMessageHandler.java:565) [spring-messaging-5.3.3.jar:5.3.3]
    at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:511) [spring-messaging-5.3.3.jar:5.3.3]
    at org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler.handleMatch(SimpAnnotationMethodMessageHandler.java:94) [spring-messaging-5.3.3.jar:5.3.3]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessageInternal(AbstractMethodMessageHandler.java:520) [spring-messaging-5.3.3.jar:5.3.3]
    at org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler.handleMessage(AbstractMethodMessageHandler.java:454) [spring-messaging-5.3.3.jar:5.3.3]
    at org.springframework.messaging.support.ExecutorSubscribableChannel$SendTask.run(ExecutorSubscribableChannel.java:144) [spring-messaging-5.3.3.jar:5.3.3]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_261]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_261]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_261]

2 个答案:

答案 0 :(得分:1)

我建议删除这个 OpenConnection 类,让 SpringBoot 在启动时为您创建数据源。

如果您真的想自己创建它,请查看文档。下面是一个例子:

@Configuration
public class DataSourceConfiguration {

   @Bean
   @ConfigurationProperties("spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
}

答案 1 :(得分:1)

我认为问题在于您在 OpenConnection 上使用的是 @Component 而不是 @Configuration

Here 你可以找到更多例子,如何在你的 Spring 应用程序中使用属性