【发布时间】: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