繁体   English   中英

如何自动装配泛型类型的 Bean<T> 在春天?

[英]How to Autowire Bean of generic type <T> in Spring?

我有一个 bean Item<T>需要在@Configuration类中自动装配。

@Configuration
public class AppConfig {

    @Bean
    public Item<String> stringItem() {
        return new StringItem();
    }

    @Bean
    public Item<Integer> integerItem() {
        return new IntegerItem();
    }

}

但是当我尝试@Autowire Item<String> ,出现以下异常。

"No qualifying bean of type [Item] is defined: expected single matching bean but found 2: stringItem, integerItem"

我应该如何在 Spring 中自动装配泛型类型Item<T>

简单的解决方案是升级到Spring 4.0,因为它会自动将泛型视为@Qualifier一种形式,如下所示:

@Autowired
private Item<String> strItem; // Injects the stringItem bean

@Autowired
private Item<Integer> intItem; // Injects the integerItem bean

事实上,您甚至可以在注入列表时自动装配嵌套泛型,如下所示:

// Inject all Item beans as long as they have an <Integer> generic
// Item<String> beans will not appear in this list
@Autowired
private List<Item<Integer>> intItems;

这是如何运作的?

新的ResolvableType类提供了实际使用泛型类型的逻辑。 您可以自己使用它来轻松导航和解析类型信息。 ResolvableType上的大多数方法本身都会返回一个ResolvableType ,例如:

// Assuming 'field' refers to 'intItems' above
ResolvableType t1 = ResolvableType.forField(field); // List<Item<Integer>> 
ResolvableType t2 = t1.getGeneric(); // Item<Integer>
ResolvableType t3 = t2.getGeneric(); // Integer
Class<?> c = t3.resolve(); // Integer.class

// or more succinctly
Class<?> c = ResolvableType.forField(field).resolveGeneric(0, 0);

在下面的链接中查看示例和教程。

希望这对你有帮助。

如果您不想升级到 Spring 4,则必须按名称自动装配,如下所示:

@Autowired
@Qualifier("stringItem")
private Item<String> strItem; // Injects the stringItem bean

@Autowired
@Qualifier("integerItem")
private Item<Integer> intItem; // Injects the integerItem bean

以下是我为回答这个问题而提出的解决方案:


        List<String> listItem= new ArrayList<>();

        ResolvableType resolvableType = ResolvableType.forClassWithGenerics(List.class, String.class);
        RootBeanDefinition beanDefinition = new RootBeanDefinition();
        beanDefinition.setTargetType(resolvableType);
        beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
        beanDefinition.setAutowireCandidate(true);

        DefaultListableBeanFactory bf = (DefaultListableBeanFactory) configurableWebApplicationContext.getBeanFactory();

        bf.registerBeanDefinition("your bean name", beanDefinition);
        bf.registerSingleton("your bean name", listItem);

Spring 自动装配策略在您的配置文件 (application.xml) 中定义。

如果没有定义,默认是按类型,spring 注入使用 JDK 反射机制。

所以列表?字符串? 和List?Item?,类型是一样的List.class,所以spring很迷茫怎么注入。

和上面的人的反应一样,你应该指向@Qualifier 告诉 spring 应该注入哪个 bean。

我喜欢 spring 配置文件来定义 bean 而不是 Annotation。

<bean>
 <property name="stringItem">
        <list>
              <....>
        </list>
 </property>

Spring 4.0 是@Qualifier 注释用法的答案。 希望这可以帮助

我相信它与泛型无关……如果您要注入两个相同类型的不同 bean,那么您需要提供一个限定符来帮助 Spring 识别它们;

... 在别处

@Configuration
@Bean
public Item stringItem() {
    return new StringItem();
}

@Bean
public Item integerItem() {
    return new IntegerItem();
}

如果您有像这样的非通用声明,那么您需要添加限定符来帮助 Spring 识别它们......

@Autowired
**@Qualifier("stringItem")**
private Item item1;

@Autowired
**@Qualifier("integerItem")**
private Item item2;

当然,在版本 4 及更高版本中,spring 通过解析器考虑通用类型,这非常酷......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM