简体   繁体   English

取决于非Spring项目的Spring项目

[英]Depending on a Spring project from a non-Spring project

I've got two Java applications that I have to combine. 我有两个Java应用程序,我必须结合起来。 One is Spring, and methods within it need to be called by the second, an Elasticsearch plugin (that I don't think can be turned into a Spring app as it already uses some form of Guice for dependency injection). 一个是Spring,其中的方法需要由第二个调用,一个Elasticsearch插件(我认为不能将其转换为Spring应用程序,因为它已经使用某种形式的Guice进行依赖注入)。

The Spring class I need to call looks like: 我需要调用的Spring类看起来像:

@Component
public class DataServiceController {

    //This is defined within a @Config
    @Autowired
    DataTypesMap dataTypesMap;

    /**
     * Create an item in the data platform
     */
    public ItemCreatedResponse createItem(String data, String dataType)
            throws IOException {
        ProcessStrategy dataStrategy = dataTypesMap.get(dataType);
        return dataStrategy.add(data);
    }

If I just add this project as a Maven dependency within the ES plugin, the Autowired dataTypesMap is always null (which is to be expected as nothing in the Elasticsearch plugin will be telling it how to autowire). 如果我只是在ES插件中将此项目添加为Maven依赖项,则Autowired dataTypesMap始终为null(这是预期的,因为Elasticsearch插件中的任何内容都不会告诉它如何自动装配)。

What can I do here? 我能在这做什么?

You can use a setter method for your autowired fields, then sets the value. 您可以为自动装配字段使用setter方法,然后设置值。

@autowired
public void setDataTypesMap (DataTypesMap  dataTypesMap ){
 this.dataTypesMap = dataTypesMap ;
}

In your application you can not autowired the bean, but you can set it. 在您的应用程序中,您无法自动装配bean,但您可以设置它。

myBean.setDataTypeMap();

The second option is initiate a the context of the spring application inside the non-spring application. 第二个选项是在非弹簧应用程序内启动弹簧应用程序的上下文。

You can see how to do it here. 你可以在这里看到如何做到这一点。

http://www.springbyexample.org/examples/intro-to-ioc-creating-a-spring-application.html http://www.springbyexample.org/examples/intro-to-ioc-creating-a-spring-application.html

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

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