gpt4 book ai didi

java - Spring应用程序加载两次

转载 作者:行者123 更新时间:2023-11-30 08:44:58 24 4
gpt4 key购买 nike

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

最佳答案

您在上面的评论中提到,您通过删除 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 中注册它们。 (取决于您在构建文件中有哪些项目)

关于java - Spring应用程序加载两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33555270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com