Spring:通过ApplicationContext访问主bean
如果我的 ApplicationContext 中有 2 个 bean 实现相同的接口,我可以在 bean 定义中将其中一个标记为主要 bean。该 bean 更适合依赖注入。
有没有一种方法可以使用 ApplicationContext 直接访问主应用程序而不需要 DI?
If I have 2 beans in my ApplicationContext implementing the same interface I can mark one of them as primary in the bean definition. This bean is prefered for dependency injection.
Is there a way to get direct access using the ApplicationContext to the primary one without DI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有许多不同的方法可以做到这一点。
在主 bean 定义中设置 Primary="true"。设置此值后,如果有多个自动布线候选者,则主要者将获得更高的优先级。
或者,如果您认为该 Bean 根本不应该自动连接到任何 Bean,则为非主 Bean 设置 autowire-candidate="false"。但是,在定义具有相同接口的另一个 bean 时需要小心,如果忘记添加它,将会再次出现多个主 bean。
通过使用命名依赖项使自动连接注释更加具体,可以使用 @javax.annotation.Resource 注释进行归档,如下所示。这将降低确定可以注入哪个 bean 的代码复杂性,因为我们可以简单地通过 id 定位 bean。
非依赖注入 (DI) 方法
如果您不喜欢使用 DI,则可以使用以下方法从 ApplicationContext 中按名称获取 bean
如果您想使用非 DI 方法,而是从 spring 中进行操作bean,如果您没有 spring 上下文的句柄来执行此操作,您可以更改您的类以实现 org.springframework.context.ApplicationContextAware 接口,这将要求您实现 setApplicationContext(ApplicationContext)方法,spring 会将句柄注入到当前应用程序上下文中,如下所示。
注意:此答案基于 Spring 2.5.x
There are many different ways to do this.
Set primary="true" in the primary bean definition. When this is set, if there are multiple candidates for auto wiring, the primary will be given the higher priority.
Alternatively, set autowire-candidate="false" for the non primary beans, if you think this bean should not be auto wired in to any bean at all. However, need to be careful when defining another bean with the same interface, as if you forget to add this, there will again be multiple primary beans.
Be more specific on the auto wiring annotations by using named dependencies, which can be archvied by using @javax.annotation.Resource annotation as shown below. This will reduce the code complexity of figuring out which bean could have been injected, as one can simply locate the bean by id.
Non Dependency Injection (DI) method
If you prefer not to use DI, you can use the following to fetch the bean by name from the ApplicationContext
If you want to do with non DI method, but from a spring bean, and if you do not have a handle to the spring context to do it, you can change your class to implement the org.springframework.context.ApplicationContextAware interface, which will require you to implement the setApplicationContext(ApplicationContext) method, in to which spring will inject the handle to the current application context as shown below.
Note: This answer is based on Spring 2.5.x
首先你需要 BeanFactory/ApplicationContext。例如,您可以通过实现 BeanFactoryAware 来检索它。假设它是 ConfigurableListableBeanFactory,您可以检查给定类型的主要内容,如下所示:
First you need your BeanFactory/ApplicationContext. You can retrieve it for example by implementing BeanFactoryAware. Assuming it is ConfigurableListableBeanFactory, you can check what's primary for given type like this:
只需将其声明为 Bean 并通过从 ApplicationContext 获取并设置它来手动设置依赖关系。但你会打破控制反转
Just declare it as Bean and set the dependency manually by fetching from ApplicationContext and setting it . But you will break Inversion of Control