spring Spring3中注入applicationContext的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9657961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Practise of injecting applicationContext in Spring3
提问by Javatar
As in the title above, I am confused about pros cons between injecting applicationContext by directly @Autowired annnotation or implementing ApplicationContextAware interface in a singleton spring bean.
正如上面的标题,我对直接通过 @Autowired 注释注入 applicationContext 或在单例 spring bean 中实现 ApplicationContextAware 接口之间的利弊感到困惑。
Which one do you prefer in which cases and why? Thanks.
在哪些情况下你更喜欢哪一个,为什么?谢谢。
回答by Sean Patrick Floyd
Actually, both are bad. Both of them tie your application to the Spring framework, thus inverting the whole inversion-of-control concept. In an ideal world, your application should not be aware of being managed by an ApplicationContext at all.
其实两者都不好。它们都将您的应用程序与 Spring 框架联系起来,从而颠倒了整个控制反转的概念。在理想的世界中,您的应用程序根本不应该知道由 ApplicationContext 管理。
Once you have chosen to violate this principle, it doesn't really matter how you do it. ApplicationContextAwareis the legacy version that has been around at least since Version 2.0. @Autowiredis a newer mechanism but they work in pretty much the same way. I'd probably go with ApplicationContextAware, because it semantically makes clear what it is about.
一旦你选择违反这个原则,你怎么做都没有关系。ApplicationContextAware是至少自 2.0版以来一直存在的旧版本。@Autowired是一种较新的机制,但它们的工作方式几乎相同。我可能会选择ApplicationContextAware,因为它在语义上清楚地说明了它的含义。
回答by sinuhepop
As @Sean Patrick Floyd says, the need of ApplicationContext is often due to a bad design. But sometimes you have no other option. In those cases I prefer the use of @Autowired because is the way I inject all other properties. So, if I use @Autowired for injecting MyRepository, why can't I use it for ApplicationContext or any other Spring bean?
正如@Sean Patrick Floyd 所说,ApplicationContext 的需要通常是由于设计不当。但有时你别无选择。在这些情况下,我更喜欢使用 @Autowired 因为这是我注入所有其他属性的方式。因此,如果我使用 @Autowired 来注入 MyRepository,为什么我不能将它用于 ApplicationContext 或任何其他 Spring bean?
I use Spring interfaces only for those things I can't do with annotations, for example BeanNameAware.
我只将 Spring 接口用于那些我不能用注释做的事情,例如 BeanNameAware。
回答by devo
If you need to get a prototype in a singleton then you can use method injection. Basically, you create an abstract method that returns the object you need and spring will return the prototype everytime you call that method. You define the "lookup-method" in your spring config. Here are some links: http://docs.spring.io/spring/docs/1.2.9/reference/beans.html#beans-factory-method-injectionhttp://java.dzone.com/articles/method-injection-spring
如果您需要在单例中获得原型,那么您可以使用方法注入。基本上,您创建一个抽象方法来返回您需要的对象,并且每次调用该方法时,spring 都会返回原型。您在 spring 配置中定义“查找方法”。这里有一些链接:http: //docs.spring.io/spring/docs/1.2.9/reference/beans.html#beans-factory-method-injection http://java.dzone.com/articles/method-注射弹簧
回答by ManuPK
Since you are not extending any of the spring classes your application is always separated from the framework. Most of the cases you will not wanted to inject the ApplicationContextas it, but will need to inject the beans defined in the ApplicationContext.
由于您没有扩展任何 spring 类,因此您的应用程序始终与框架分离。大多数情况下,你会不会想注入ApplicationContext,因为它,但将需要注入中定义的豆类ApplicationContext。
The best case is always to stick to the bare minimum, until and unless you have any specific requirement and this is very simple with spring.
最好的情况是始终坚持最低限度,直到并且除非您有任何特定要求,这对于 spring 非常简单。
So either,
所以要么,
Annotate your beans and
scanthem inapplication context, then use@Autowireto wire them up.Use
application contextto wire your bean expediencies(old xml style configs). You can use@Autowirewith this approach also.
在 中注释您的 bean 和
scan它们application context,然后使用@Autowire它们将它们连接起来。用于
application context连接您的 bean 权宜之计(旧的 xml 样式配置)。您也可以使用@Autowire这种方法。
When you want to control the bean life cycle, you can read the API and customize it, but most of the time these general settings will do the job.
当您想要控制 bean 生命周期时,您可以阅读 API 并对其进行自定义,但大多数情况下这些常规设置可以完成这项工作。
Here are some examples.
这里有些例子。

