Bean order when autowired into list

瘦欲@ 提交于 2019-12-02 07:09:01

问题


I have defined an interface IWorker and some implementations of it as WorkerA and WorkerB, both annotated with @Component.

I then autowire them into my app via:

@Autowired
private List<IWorker> workers = new ArrayList<IWorker>();
  • From what does the order the workers are put into the list depend on?

  • How can I let additional WorkerC and WorkerD (also implementations of IWorker) not annotated with @Component be autowired into the same list via my applicationContext.xml?

  • Is the order of WorkerC and WorkerD from the xml preserved?

  • Is there a rule I can rely on in which order worker A, B, C and D will be put into the list?


回答1:


If you want to order these dependencies in the List injected by Spring, then use @Order annotation.



来源:https://stackoverflow.com/questions/28012388/bean-order-when-autowired-into-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!