Dim*_*ims 5 java spring applicationcontext
可以在@Autowired类和@QualifierSpring中使用bean 。
如何以编程方式做同样的事情?即搜索上下文的bean给它的类和限定符?
我看到了很多getBean()方法,但没有一个方法明确声明它可以做到这一点。
您可以使用BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier):
/**
* Obtain a bean of type {@code T} from the given {@code BeanFactory} declaring a
* qualifier (e.g. via {@code <qualifier>} or {@code @Qualifier}) matching the given
* qualifier, or having a bean name matching the given qualifier.
* @param beanFactory the BeanFactory to get the target bean from
* @param beanType the type of bean to retrieve
* @param qualifier the qualifier for selecting between multiple bean matches
* @return the matching bean of type {@code T} (never {@code null})
* @throws NoUniqueBeanDefinitionException if multiple matching beans of type {@code T} found
* @throws NoSuchBeanDefinitionException if no matching bean of type {@code T} found
* @throws BeansException if the bean could not be created
* @see BeanFactory#getBean(Class)
*/
public static <T> T qualifiedBeanOfType(BeanFactory beanFactory, Class<T> beanType, String qualifier)
throws BeansException;
Run Code Online (Sandbox Code Playgroud)
我认为这正是您所需要的。
如果您希望 Spring 以@Autowired编程方式处理字段/属性,您需要AutowireCapableBeanFactory从您的实例中获取实例ApplicationContext并调用factory.autowireBean(myClassInstanceWithBeansToAutowire);.
| 归档时间: |
|
| 查看次数: |
5333 次 |
| 最近记录: |