gpt4 book ai didi

Java spring boot 配置属性未加载 EnableConfigurationProperties 不起作用

转载 作者:行者123 更新时间:2023-12-01 17:58:40 24 4
gpt4 key购买 nike

我在我的 spring boot application.properties 中获得了值,但我无法在我的 java 类中使用它。 当我执行 System.out.println("@@@@ "+onlinePajakProperties.getAuthentication()); 数据未显示,空指针错误。

应用程序属性

pajak.authentication = tLFGed7iTD1rztqn9z8aMS0PkCPqBW0i


@EnableScheduling
@SpringBootApplication
@EnableConfigurationProperties({
OnlinePajakProperties.class
})
public class Application {

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

}


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "pajak")
public class OnlinePajakProperties {
private String authentication;

public String getAuthentication() {
return authentication;
}
}


@Service
public class APISservice {

String urlPath;

@Autowired
private OnlinePajakProperties onlinePajakProperties;

public void getvalue(){
if(onlinePajakProperties == null) {
System.out.println("onlinePajakProperties is NUll");
}
//this line shows no data
System.out.println("@@@@ "+onlinePajakProperties.getAuthentication());
}
}

最佳答案

您应该为身份验证属性添加setter。

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "pajak")
public class OnlinePajakProperties {

@Getter
@Setter
private String authentication;
}

关于Java spring boot 配置属性未加载 EnableConfigurationProperties 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60673092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com