Home > Software design >  In my Springboot project, how to use @Value annotation to get the property value in the Properties f
In my Springboot project, how to use @Value annotation to get the property value in the Properties f

Time:10-10

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 structure

class A

class A

Class B

Class B

properteis

properteis

But if I turn Class A into a test class, I can get the results I want the result as follow add the @SpringBootTest on Class A

CodePudding user response:

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;
}
  • Related