Doesn't Spring really support Interface injection at all?

时光总嘲笑我的痴心妄想 提交于 2019-12-06 01:23:28

问题


I know that Spring doesn't supports Interface injection and I've read that many a times.

But today as I came across an article about IOC by Martin Fowler (link), it seems using ApplicationContextAware in Spring is some what similar to the Interface injection.

when ever Spring' context reference is required in our Spring bean, we'll implement ApplicationContextAware and will implement the setApplicationContext(ApplicationContext context) method, and we'll include the bean in the config file. Is not this the same as Interface injection, where where telling the Spring to inject (or), say, pass the reference of the context into this bean?

Or I m missing something here? Thanks for any information!

ManiKanta


回答1:


If you mean interface injection as defined on wikipedia, spring supports it out of the box for ResourceLoaders, ApplicationContexts, MessageSource, and others, with the interfaces ResourceLoaderAware, ApplicationContextAware, MessageSourceAware, respectively.

It is also possible to extend this mechanism with new interfaces an depedencies by registering a BeanPostProcessor.

The Spring reference manual explains this capability (and when one should (not) use it) quite clearly. BTW, I have generally found the spring reference manual to be much more reliable than what 'someone on the internet' says.



来源:https://stackoverflow.com/questions/2827147/doesnt-spring-really-support-interface-injection-at-all

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