首页 > 解决方案 > 堆叠@Bean 和@Autowired 注解

问题描述

我正在尝试理解以下代码

@Bean
@ConditionalOnMissingBean(name = "monitor")
@ConditionalOnBean(name={"identity"})
@Autowired
public MyObject myObject(@Qualifier("identity") Identity identity) {
    return new MyObject(clientFactory);
}

@Autowired 的用途是什么?

在我看来,这似乎是多余的。猜测作者可能认为它需要支持身份注入。但是,AFAIK,@Bean 会这样做。

- - 编辑 - -

可能需要提一下,我之前看到的示例在 @Configuration 类中。

@Bean
public AnotherObject anotherObject(Environment env) {
    return new AnotherObject(env);
}

标签: javaspringautowiredspring-bean

解决方案


推荐阅读