@ConfigurationProperties 未在 Spring 应用程序上为字段设置值

问题描述 投票:0回答:1

按照 Baeldung 教程,我无法使代码工作。该字段未填充。我创建了一个简单的 Spring 项目,并像教程一样进行配置,并使用 @Scheduled 函数进行测试,但在访问该字段时得到空值。

SpringBoot 版本:'2.3.1.RELEASE'

Build.gradle 依赖项

dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.2.1.RELEASE', ext: 'pom'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}

配置属性

@ConfigurationProperties(prefix = "mail")
public class ConfgProperties {

    private String hostName;
    private int port;
    private String from;

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }
}

application.yml

mail:
  hostname: [email protected]
  port: 9000
  from: [email protected]

预定中

ConfgProperties p;

@Scheduled(fixedDelay = 5000)
private void schedule() {
    LOGGER.info("P: {}", p.getFrom()); // NULL HERE
}

主要应用类

@SpringBootApplication
@EnableConfigurationProperties
public class SpringDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringDemoApplication.class, args);
    }

}

空异常

java.lang.NullPointerException: null
    at com.sample.springdemo.job.JobTest.schedule(JobTest.java:30) ~[main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_252]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_252]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_252]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_252]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_252]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_252]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_252]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_252]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_252]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_252]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_252]
java spring spring-boot spring-annotations
1个回答
0
投票

您可以通过两种方式解决。更喜欢第一个选项

  1. 我们使用@Configuration,以便Spring在应用程序上下文中创建一个Spring bean。
    @Configuration 
    @ConfigurationProperties(prefix = "mail")
    public class ConfgProperties {
    
        private String hostName;
        private int port;
        private String from;
  1. 定义属性名称如下
    @SpringBootApplication
    @EnableConfigurationProperties(ConfgProperties.class)
    public class SpringDemoApplication {
© www.soinside.com 2019 - 2024. All rights reserved.