为什么注入bean后得到空值?

Why do I get null values after injecting bean?

当我尝试在 Operation.class 中注入 packagePropertiesList Bean 时,我从属性文件中获取值。但是当我使用 operation.removeStudentFromList() 时,我只得到空值。你看这里有什么问题吗?

values while injecting beans

@SpringBootApplication
public class LearningCenterApplication {


    public static void main(String[] args) {
        SpringApplication.run(LearningCenterApplication.class, args);
        ApplicationContext context =
                new AnnotationConfigApplicationContext(Config.class, Operations.class, PackageProperties.class);
        Operations operations = context.getBean(Operations.class);
        operations.removeStudentFromList();
    }

    @Bean
    List<PackageProperties> packagePropertiesList(List<PackageProperties> packageProperties) {
        System.out.println(packageProperties);
        return packageProperties;
    }

    @Bean
    @ConfigurationProperties(prefix = "remove")
    public PackageProperties removeMethod() {
        return new PackageProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "add")
    public PackageProperties addMethod() {
        return new PackageProperties();
    }

}
@Component
public class Operations {

    private List<PackageProperties> packagePropertiesList;


    @Autowired
    public Operations(List<PackageProperties> packagePropertiesList) {
        this.packagePropertiesList = packagePropertiesList;
    }

    public void removeStudentFromList() {
        System.out.println(packagePropertiesList);
    }
}
public class PackageProperties {
    private String packageName;
    private String className;
    private String methodName;



    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    @Override
    public String toString() {
        return packageName + "." + className + "." + methodName;
    }
}

application.properties

remove.packageName = org.epam.operations
remove.className = Operations
remove.methodName = removeStudentFromList()
add.packageName = org.epam.operations
add.className = Operations
add.methodName = addStudent()

[null.null.null] — 调用 operations.removeStudentFromList() 时输出

您正在 Spring Boot(使用 new AnnotationConfigApplicationContext())已经创建的应用程序上下文旁边创建一个额外的 ApplicationContext。删除它并使用现有的上下文来获取一个 bean,或者更确切地说看一下在ApplicationRunner界面,如果你想运行应用程序启动后自动编码。

整个 属性 处理仅适用于由 Spring Boot 创建和管理的应用程序上下文,不适用于您自己创建的应用程序上下文。