2017-03-06 76 views
1

我有兩個必須組合的Java應用程序。一個是Spring,其中的方法需要被第二個調用,一個Elasticsearch插件(我不認爲它可以變成一個Spring應用程序,因爲它已經使用某種形式的Guice進行依賴注入)。根據來自非Spring項目的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); 
    } 

如果我只是添加這個項目作爲ES插件內的Maven的依賴,將自動裝配Autowired dataTypesMap總是空(這是可以預料的是什麼在Elasticsearch插件將告訴它如何自動裝配)。

我能在這裏做什麼?

回答

0

您可以爲自動佈線字段使用setter方法,然後設置該值。

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

在你的應用程序中,你不能自動裝配bean,但你可以設置它。

myBean.setDataTypeMap(); 

第二個選項是啓動非彈簧應用程序內的spring應用程序的上下文。

你可以在這裏看到如何做到這一點。

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