[英]In my Springboot project, how to use @Value annotation to get the property value in the Properties file
There are two classes and a configuration file, but they are all in the test directory I want to use the @Value annotation in class A to get the attribute value, and then assemble class A in class B, use the method in class A, I want to output the value obtained from the configuration file, but in fact it is all null How can i fix it!!! eg:有两个类和一个配置文件,但是都在test目录下 我想在class A中使用@Value注解获取属性值,然后在class B中组装class A,使用class A中的方法,我想output是从配置文件中获取的值,但实际上都是null 怎么修改!!! eg:
class structure: class结构:
class A class 一个
Class B Class乙
properteis属性
But if I turn Class A into a test class, I can get the results I want the result as follow但是如果我把 Class A 变成测试 class,我可以得到我想要的结果如下
You need to autowire class A
in class B
.您需要在
class B
中自动装配class A
。 Instead of using the spring managed bean with injected @Value
you are creating a new class resulting in the variables in class A
to be null.您不是使用带有注入
@Value
的 spring 托管 bean,而是创建一个新的 class,导致class A
中的变量为 null。
@SpringBootTest
@Import(A.class)
public class B {
@Autowire
private final A a;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.