简体   繁体   English

无法访问 springboot 组件中的 application.yml 属性

[英]Not able to access application.yml properties in springboot component

I have an application.yml file which contains properties as below我有一个application.yml文件,其中包含如下属性

NAME:
   CLASS:
     ID: ABC123456

and here is my spring boot component class这是我的 Spring Boot 组件类

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class  ProcessMe {

@Value("${NAME.CLASS.ID}")
String StuId;

public boolean IsRightOrWrong(){
    System.out.println(StuId);
}
}

In above componenent System.out.println(StuId);在上面的组件System.out.println(StuId); is always coming as null.总是为空。 When i am tying to call this function using Junit test class.当我想使用 Junit 测试类调用这个函数时。 What's wrong in the above code?上面的代码有什么问题?

I suppose this solution to define in constructor like我想这个解决方案在构造函数中定义

@Component
@Slf4j
public class  ProcessMe {

String StuId;

@Autowired
ProcessMe(@Value("${NAME.CLASS.ID}") String StuId) {
    this.StuId = StuId;
}
public boolean IsRightOrWrong(){
    System.out.println(this.StuId);
}
}

Hope useful希望有用

If you can start the application then I believe that your configuration is correct.如果您可以启动应用程序,那么我相信您的配置是正确的。 I mean that StuId have some value.我的意思是StuId有一些价值。

The problem is how you testing When i am tying to call this function using Junit test class. What's wrong in the above code?问题是你如何测试When i am tying to call this function using Junit test class. What's wrong in the above code? When i am tying to call this function using Junit test class. What's wrong in the above code?

It seems that you're calling from the unit test.看来您是从单元测试中调用的。 Then you need to clone application.yaml to test profile then your test container can read data, otherwise you have to mock your configuration.然后你需要克隆 application.yaml 来测试配置文件,然后你的测试容器可以读取数据,否则你必须模拟你的配置。

btw: Java Object property should using camel case lol顺便说一句:Java 对象属性应该使用驼峰式大小写 lol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在springboot中从application.yml中读取带有特殊字符的属性 - How to read properties with special characters from application.yml in springboot 使用application.yml配置SpringBoot - SpringBoot Configuration with application.yml java 8 Springboot 如何在注释中使用 application.yml 文件中的属性? - java 8 Springboot How to use properties from application.yml file in annotations? 通过@Value 获取属性并以编程方式将其放入 application.yml 中。 Java 与 SpringBoot - Get properties by @Value and put it into application.yml programmatically. Java with SpringBoot 获取应用程序名称Springboot application.yml时获得NullPointerException - Got NullPointerException when getting application name Springboot application.yml 从 Micronaut 中的 application.yml 访问属性 - Access Property from application.yml in Micronaut Springboot-从application.yml注入取决于方法名称 - Springboot - injection from application.yml depending method name Spring Boot - 从 application.yml 和 application.properties 注入 - Spring Boot - inject from application.yml and application.properties Spring Boot application.yml和application.properties - Spring Boot application.yml and application.properties 将 application.yml 文件转换为 application.properties - convert application.yml file to application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM