之前用xml配置的时候,可以处理类中的List,Set,Map属性,那用注解怎么处理?
来源:5-8 元数据注解
只能卷了
2023-03-10 19:36:23
问题描述:
之前用xml配置的时候,可以处理类中的List,Set,Map属性,那用注解怎么处理?
比如有一个School类,它有List<Student> students。如果在Student类上我们使用@Component,然后再School类的students属性上使用什么注解才能注入多个student呢?
比如在xml中我们可以为School类写一个bean,然后在<bean>里描述<property name="students">的<list>,然后在list中使用ref bean="student1",就可以添加一个id为student1的Student bean。
但是我们如果在注解中怎么做呢?比如Student类上加了@Component,那在IoC容器里也就只有一个bean id是student的Student对象。那我们用什么注解可以给List<Student> students 注入多个不同的Student对象呢?
1回答
同学你好,同学提出的需求是无法通过@Component注解完成的,需要通过@Configuration注解声明配置类,在其中使用@Bean注解来进行手动注入。
祝学习愉快~
相似问题