2015-11-05 183 views
1

我是Spring的新手,試圖獲得一個工作示例。但是我的應用程序每次啓動時會加載兩次。我認爲這可能是一個背景問題,因爲我的互聯網研究,我只有一個context.xml。春季應用程序加載兩次

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:annotation-config/> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:environment.properties</value> 
      </list> 
     </property> 
     <property name="ignoreUnresolvablePlaceholders" value="false"/> 
    </bean> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>classpath:environment.properties</value> 
      </list> 
     </property> 
     <property name="ignoreUnresolvablePlaceholders" value="false"/> 
    </bean> 

    <bean name="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" /> 

    <bean name="restTemplate" class="org.springframework.web.client.RestTemplate"> 
     <property name="requestFactory" ref="requestFactory" /> 
    </bean> 

    <bean name="requestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"> 
     <property name="connectTimeout" value="10000" /> 
     <property name="readTimeout" value="10000" /> 
    </bean> 

    <bean name="httpClient" class="org.apache.http.client.HttpClient" factory-bean="requestFactory" factory-method="getHttpClient"/> 

    <bean name="TraderApplication" class="net.mrmoor.TraderApplication"/> 

    <bean name="API" class="com.iggroup.api.API"/> 
    <bean name="LightStreamerComponent" class="com.iggroup.api.streaming.LightStreamerComponent"/> 

</beans> 

我的TraderApplication類的代碼是:

... skipped imports .... 

@SpringBootApplication 
public class TraderApplication implements CommandLineRunner{ 

private static final Logger log = LoggerFactory.getLogger(TraderApplication.class); 

@Autowired 
protected ObjectMapper objectMapper; 

@Autowired 
private API api; 

@Autowired 
private LightStreamerComponent lightStreamerComponent = new LightStreamerComponent(); 

private AuthenticationResponseAndConversationContext authenticationContext = null; 
private ArrayList<HandyTableListenerAdapter> listeners = new ArrayList<HandyTableListenerAdapter>(); 

public static void main(String args[]) { 
    SpringApplication.run(TraderApplication.class, args); 
} 

@Override 
public void run(String... args) throws Exception { 
    try { 
     if (args.length < 2) { 
      log.error("Usage:- Application identifier password apikey"); 
      System.exit(-1); 
     } 

     String identifier = args[0]; 
     String password = args[1]; 
     String apiKey = args[2]; 
     ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/public-api-client-spring-context.xml"); 
     TraderApplication app = (TraderApplication) applicationContext.getBean("TraderApplication"); 
     app.run(identifier, password, apiKey); 
    } catch (Exception e) { 
     log.error("Unexpected error:", e); 
    } 
} 
+0

在'run()'中放置一個斷點並檢查完整的堆棧跟蹤 – Raffaele

+3

你正在開始你的班級兩次。一旦通過運行main方法,然後再次通過加載另一個上下文來創建一個'net.mrmoor.TraderApplication'。爲什麼你甚至自己構造一個上下文,這基本上擊敗了使用彈簧引導的全部目的。 –

+0

感謝您的回答!我複製了上下文文件以使其正常工作:(但通過您的提示,我設法通過將代碼更改爲:this.run(identifier,password,apiKey);並刪除其餘部分來解決問題:) – mrmoor

回答

1

您在您的評論提及上述通過刪除SpringApplication.run(TraderApplication.class,參數)有這方面的工作;但是這會從你的應用程序中刪除spring-boot,所以我會假設你的問題有一個[spring-boot]的標籤,這不是你想要的。所以這裏有一種可以用xml配置bean的替代方法。

@ImportResource({"classpath*:public-api-client-spring-context.xml"}) //Proper way to import xml in Spring Boot 
@SpringBootApplication 
public class TraderApplication implements CommandLineRunner { 

    ...code you had before goes here 

    @Autowired 
    TraderApplication app; 

    @Override 
    public void run(String... args) throws Exception { 
     .. your parsing logic here 

     app.run(identifier, password, apiKey); //Now uses the autowired instance 

    } 
} 

你沒有列出你的pom.xml或的build.gradle但要記住,你已經在你的背景下XML註冊的組件可以在春天開機自動配置是非常重要的,你可能不需要自己進行註冊(取決於你的構建文件中有哪些項目是你的首發)

+0

謝謝!這正確的解決了我的問題! – mrmoor