简体   繁体   中英

Depending on a Spring project from a non-Spring project

I've got two Java applications that I have to combine. 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).

The Spring class I need to call looks like:

@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).

What can I do here?

You can use a setter method for your autowired fields, then sets the value.

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

In your application you can not autowired the bean, but you can set it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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