首页 文章

class字段同时具有@Autowired注释和右手赋值

提问于
浏览
2

在Spring Boot的源代码中,我对具有 @Autowired 注释和右手赋值的类字段感到困惑 .

@Autowired
private ResourceProperties resourceProperties = new ResourceProperties();

位于https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java

换句话说,此字段配置为字段注入,但也使用 new 运算符直接分配 . 实际分配给这个变量的是什么?

1 回答

  • 8

    创建实例时,初始化表达式的值将分配给该字段 . 当 AutowiredBeanPostProcessor 处理该字段时,它将为其分配一个新值 .

    如果你希望在Spring的上下文之外运行相同的类,你可以编写这样的代码 .

相关问题