@ConfigurationProperties在Spring应用程序中未将值设置到字段上。

huangapple 未分类评论44阅读模式
英文:

@ConfigurationProperties not setting values to fields on Spring application

问题

代码部分不提供翻译。

英文:

Following the Baeldung tutorial, I can't make the code works. The field is not be populated. I've created a simple Spring project and configure like the tutorial and tested using a @Scheduled function but is gotting a null value when access the field.

SpringBoot version: '2.3.1.RELEASE'

Build.gradle dependencies

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'
}
}

ConfgProperties

@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: host@mail.com
  port: 9000
  from: mailer@mail.com

Scheduled

ConfgProperties p;

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

Main application class

@SpringBootApplication
@EnableConfigurationProperties
public class SpringDemoApplication {

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

}

Null exception

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]

答案1

得分: 0

你可以通过两种方式来解决。更倾向于第一种选项:

  1. 我们使用 @Configuration 来使 Spring 在应用程序上下下文中创建一个 Spring Bean。

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

     private String hostName;
     private int port;
     private String from;
    
  2. 如下所示定义属性名称:

    @SpringBootApplication
    @EnableConfigurationProperties(ConfgProperties.class)
    public class SpringDemoApplication:

英文:

You can solve via two ways. Would prefer the first option

  1. We use @Configuration so that Spring creates a Spring bean in the application context.

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

     private String hostName;
     private int port;
     private String from;
    
  2. Define property name as below

    @SpringBootApplication
    @EnableConfigurationProperties(ConfgProperties.class)
    public class SpringDemoApplication {

huangapple
  • 本文由 发表于 2020年7月23日 22:57:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63057205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定