Java Spring引导和Spring集成。问题
我有一个SpringBoot应用程序。我有一个专用服务器,在那里我将通过HTTP GET请求读取数据。我为Spring集成模块配置了http-outbound-config.xml文件。 运行以下代码时,一切正常: http-outbound-config.xmlJava Spring引导和Spring集成。问题,java,spring,spring-boot,spring-integration,spring-integration-http,Java,Spring,Spring Boot,Spring Integration,Spring Integration Http,我有一个SpringBoot应用程序。我有一个专用服务器,在那里我将通过HTTP GET请求读取数据。我为Spring集成模块配置了http-outbound-config.xml文件。 运行以下代码时,一切正常: http-outbound-config.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-http="http://www.springframework.org/schema/integration/http"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration https://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/http https://www.springframework.org/schema/integration/http/spring-integration-http.xsd">
<int:channel id="requestChannel"/>
<int:channel id="replyChannel">
<int:queue capacity='10'/>
</int:channel>
<int-http:outbound-gateway id="outboundGateway"
request-channel="requestChannel"
url="http://server/API.jsp?id=1"
http-method="GET"
expected-response-type="java.lang.String"
charset="UTF-8"
reply-channel="replyChannel"/>
</beans>
集成类:
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
@Configuration
public class Integration {
public void start() {
ClassPathXmlApplicationContext
context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/http-outbound-config.xml");
context.start();
MessageChannel requestChannel = context.getBean("requestChannel", MessageChannel.class);
PollableChannel replyChannel = context.getBean("replyChannel", PollableChannel.class);
Message<?> message = MessageBuilder.withPayload("").build();
requestChannel.send(message);
Message<?> receivedMsg = replyChannel.receive();
System.out.println("RESULT IS : " + receivedMsg.getPayload());
}
}
封装测试;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.boot.autoconfigure.EnableAutoConfiguration;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.ImportResource;
导入org.springframework.context.support.ClassPathXmlApplicationContext;
导入org.springframework.messaging.Message;
导入org.springframework.messaging.MessageChannel;
导入org.springframework.messaging.PollableChannel;
导入org.springframework.messaging.support.MessageBuilder;
导入org.springframework.stereotype.Component;
导入org.springframework.web.client.rest模板;
@组成部分
@配置
公共类集成{
公开作废开始(){
ClassPathXmlApplicationContext
context=new ClassPathXmlApplicationContext(“classpath:META-INF/spring/integration/http outbound config.xml”);
context.start();
MessageChannel requestChannel=context.getBean(“requestChannel”,MessageChannel.class);
PollableChannel replyChannel=context.getBean(“replyChannel”,PollableChannel.class);
Message Message=MessageBuilder.withPayload(“”.build();
发送(消息);
Message receivedMsg=replyChannel.receive();
println(“结果是:+receivedMsg.getPayload());
}
}
但是,当我尝试自动连接MessageChannel和PollableChannel时,我收到一个空指针异常。
集成类(非工作示例):
封装测试;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.boot.autoconfigure.EnableAutoConfiguration;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.ImportResource;
导入org.springframework.context.support.ClassPathXmlApplicationContext;
导入org.springframework.messaging.Message;
导入org.springframework.messaging.MessageChannel;
导入org.springframework.messaging.PollableChannel;
导入org.springframework.messaging.support.MessageBuilder;
导入org.springframework.stereotype.Component;
导入org.springframework.web.client.rest模板;
@组成部分
@配置
公共类整合{
@自动连线
@限定符(“请求通道”)
消息通道请求通道;
@自动连线
@限定符(“replyChannel”)
PollableChannel-replyChannel;
公开作废开始(){
Message Message=MessageBuilder.withPayload(“”.build();
发送(消息);
Message receivedMsg=replyChannel.receive();
println(“结果是:+receivedMsg.getPayload());
}
}
newintegration()代码>,您肯定不会进行依赖项注入,因为不涉及控件容器的反转。尽管现在还不清楚,如果您已经进行了spring引导,并且正确地执行了@ImportResource
,那么为什么需要新的ClassPathXmlApplicationContext(“classpath:META-INF/spring/integration/http outbound config.xml”)
@ImportResource
。然后您需要访问SpringApplication.run()
和getBean(Integration.class)
的ApplicationContext
来调用start()
方法。但是,您完全需要忘记newintegratio()
。Spring将为该集成管理一个bean,然后依赖注入将开始工作
replyChannel.receive()
…进行阻止。。。尽管这应该在单独的SO线程中解决
还可以阅读一些关于依赖注入和控制反转的文档:Thank you@artem bilan
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
@Configuration
public class Integration {
public void start() {
ClassPathXmlApplicationContext
context = new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/http-outbound-config.xml");
context.start();
MessageChannel requestChannel = context.getBean("requestChannel", MessageChannel.class);
PollableChannel replyChannel = context.getBean("replyChannel", PollableChannel.class);
Message<?> message = MessageBuilder.withPayload("").build();
requestChannel.send(message);
Message<?> receivedMsg = replyChannel.receive();
System.out.println("RESULT IS : " + receivedMsg.getPayload());
}
}
package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
@Configuration
public class Integration {
@Autowired
@Qualifier("requestChannel")
MessageChannel requestChannel;
@Autowired
@Qualifier("replyChannel")
PollableChannel replyChannel;
public void start() {
Message<?> message = MessageBuilder.withPayload("").build();
requestChannel.send(message);
Message<?> receivedMsg = replyChannel.receive();
System.out.println("RESULT IS : " + receivedMsg.getPayload());
}
}