[英]Spring JavaConfig equivalent of referring to beans by name
在 Spring XML 配置中,我可以使用 ref="bean_name" 语法通过 ID 或名称来引用 bean。
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
是否有在 Java 配置中使用 @Bean 注释上的 name 属性实现此目的的等效方法? 我已经使用了@Named
注释(javax.inject.Named)
并且在其他地方看到了@Qualifier
建议,但是如果我们已经在@Bean
注释上有一个 name 属性,是否真的需要单独的注释?
@Qualifier
在这里使用是正确的。
@Autowired
@Qualifier("bean_name")
private Beantype var;
并声明您的组件
@Component(value = "bean_name")
public class Beantype {...}
至少这对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.