简体   繁体   English

Spring:如何手动注入不受实际应用程序管理的bean?

[英]Spring : How to inject manually a bean which is not managed by actual application?

Here the context : 这里的上下文:

  • we have a java library, which is a factory code. 我们有一个Java库,这是工厂代码。
  • this library is deployed directly on Tomcat 该库直接部署在Tomcat上
  • Application "A", "B" & "C" use this library (jar) to compile, and it is the deployed version on Tomcat which is used when an application call it. 应用程序“ A”,“ B”和“ C”使用此库(jar)进行编译,它是Tomcat上的已部署版本,当应用程序调用它时使用。

In the library, we have these packages : - old.service - old.service.impl - new.service - new.service.impl 在库中,我们有以下软件包:-old.service-old.service.impl-new.service-new.service.impl

The old services are some classic classes with setters. 旧服务是一些带有二传手的经典类。 It is a spring bean declared in XML configuration of application "A". 它是在应用程序“ A”的XML配置中声明的spring bean。

In the new services, we have an annotated class (@Service) with some @Autowired attributes in order to be managed by application "B" & "C" which have an autoscan in XML configuration. 在新服务中,我们有一个带有一些@Autowired属性的带注释的类(@Service),以便由在XML配置中具有自动扫描功能的应用程序“ B”和“ C”进行管理。

We would like to change the implementation of a old services, in order to use the new one without changing anything in application "A". 我们希望更改旧服务的实现,以便使用新服务而不更改应用程序“ A”中的任何内容。 For that, we can call the new class from the older. 为此,我们可以从旧的类中调用新的类。 But the pb is Spring...... 但是pb是Spring ......

How can we instanciate the new class, and the @autowired attributes ? 我们如何实例化新类和@autowired属性? Can we instanciate manually the new class in older class, and instanciate attributes by reflection ? 我们可以手动实例化旧类中的新类,并通过反射实例化属性吗?

Thank you. 谢谢。

ps: there is no XML configuration in the java library. ps:java库中没有XML配置。

Two ways: 两种方式:

  1. In app A where you instantiate old.service.impl Spring bean inject reference to new.service.impl into it 在应用程序A中,您实例化old.service.impl,Spring bean将对new.service.impl的引用注入其中

    < bean class="old.service.impl"> < property name="newService" ref="newServiceBean"/> < /bean> < bean class="new.service.impl" id="newServiceBean" /> <bean class =“ old.service.impl”> <属性名=“ newService” ref =“ newServiceBean” /> </ bean> <bean class =“ new.service.impl” id =“ newServiceBean” />

Obviously this means you will have to modify your old.service.impl to add setter for "newService", then use it in old service impl code 显然,这意味着您将必须修改old.service.impl以为“ newService”添加设置器,然后在旧服务的impl代码中使用它

  1. Get "new.service.impl" reference directly from web application context: 直接从Web应用程序上下文获取“ new.service.impl”引用:

    WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean(newServiceImpl.class); WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean(newServiceImpl.class);

You will need to obtain ServletContext in this case. 在这种情况下,您将需要获取ServletContext。 One way to do it is to get it from HttpRequest 一种方法是从HttpRequest中获取它

Option 1 is preferred - you are not relying on running inside a servlet then, and requires very few changes in your code. 选项1是首选-您不依赖于在servlet内部运行,并且几乎不需要更改代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM