[英]How to autowire map
I have very simple problem. 我有一个非常简单的问题。 But I can't find out where I am wrong.
但是我找不到我错了。 I use spring 2.0.2.
我使用spring 2.0.2。 I want to set property of Map myTasks using Autowire, but as a result I have myTasks = null.
我想使用Autowire设置Map myTasks的属性,但是结果是myTasks = null。
my code: In file spring: 我的代码:在文件春天:
<bean id="Service" class="Service" autowire="byType"/>
<bean id="FirstTask" class="FirstTask"/>
<bean id="SecondTask" class="SecondTask"/>
where FirstTask, SecondTask extends MyTask 其中FirstTask,SecondTask扩展了MyTask
Service.class 服务类
class Service{
private Map<String, MyTask> myTasks;
public Map<String, MyTask> getMyTasks(){return myTasks;}
public void MyTasks(Map<String, MyTask> myTasks){this.myTasks = myTasks;}}
As far as I understand, Spring 2.0.2 doesn't support autowiring a map of beans this way. 据我了解,Spring 2.0.2不支持以这种方式自动装配bean的地图。 You can use
ApplicationContextAware
and getBeansOfType()
instead: 您可以改用
ApplicationContextAware
和getBeansOfType()
:
public class Service implements ApplicationContextAware {
public void setApplicationContext(ApplicationContext ctx) {
myTasks = (Map<String, MyTask>) ctx.getBeansOfType(MyTask.class);
}
...
}
How is myTasks declared in the Spring config file ? 如何在Spring配置文件中声明myTasks? If you are not sure , you could use use util:map to achieve this
如果不确定,则可以使用util:map实现此目的
Refer http://static.springsource.org/spring/docs/2.0.2/reference/xsd-config.html#xsd-config-body-schemas-util-map 请参阅http://static.springsource.org/spring/docs/2.0.2/reference/xsd-config.html#xsd-config-body-schemas-util-map
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.