gpt4 book ai didi

java - 如何以编程方式获取带有类和限定符的 bean?

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:17 26 4
gpt4 key购买 nike

有可能@Autowired bean 类和@Qualifier在 Spring 。

如何以编程方式做同样的事情? IE。为 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;

我认为这正是您所需要的。

关于java - 如何以编程方式获取带有类和限定符的 bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37943409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com