简体   繁体   中英

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:

class structure:

班级结构

class A

A类

Class B

B类

properteis

属性

But if I turn Class A into a test class, I can get the results I want the result as follow在 A 类上添加 @SpringBootTest

You need to autowire class A in class B . 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.

@SpringBootTest
@Import(A.class)
public class B {

  @Autowire
  private final A a;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM