【问题标题】:Spring Application loads twiceSpring 应用程序加载两次
【发布时间】:2015-11-05 21:37:42
【问题描述】:

我是 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);
    }
}

【问题讨论】:

  • run() 中放置一个断点并检查完整的堆栈跟踪
  • 你开始上课两次。一次通过运行 main 方法,然后再次通过加载另一个上下文创建 net.mrmoor.TraderApplication。你为什么还要自己构建一个上下文,这基本上超出了使用 spring boot 的全部目的。
  • 感谢您的回答!我复制了上下文文件以使其工作:(但是根据您的提示,我设法通过将代码更改为:this.run(identifier, password, apiKey); 并删除其余部分来解决问题;)
  • 仅供参考,您已经定义了两次 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。
  • 我在 Spring Boot 应用程序启动两次时遇到了同样的问题。我的错误是我用@ComponentScan("...")@SpringBootApplication 注释了主类。但是,ComponentScan 已包含在 SpringBootApplication 中。因此,它被执行了两次。

标签: java spring spring-mvc spring-boot


【解决方案1】:

您在上面的 cmets 中提到您通过删除 SpringApplication.run(TraderApplication.class, args);但这会从您的应用程序中删除 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 中注册的组件可能会在 Spring Boot 中自动配置,您可能不需要自己在 xml 中注册它们。 (取决于您的构建文件中有哪些项目)

【讨论】:

    猜你喜欢
    • 2014-08-18
    • 2012-10-04
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多