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
Class B
properteis
But if I turn Class A into a test class, I can get the results I want the result as follow
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;
}