Java Spring引导和Spring集成。问题

Java 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

我有一个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/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());
}
}
  • 问题1:为什么自动布线不起作用
  • 问题2:从专用服务器获取数据并将其保存到数据库中的最佳方法是什么?这样的配置可以吗?我将为响应创建一个模型类,然后通过JPA将其保存到DB中
  • 问题3:我需要在异步模式下从服务器读取数据。如何在Spring Boot中实现它?所以这里的主要思想是,我将从UI接收一个POST方法,并将启动与web服务的集成。集成完成后,我需要通知用户
  • 问题4:也许骆驼是这里最好的解决方案
  • 堆栈:Java11、Spring引导、thymeleaf+引导

    提前谢谢你的回答

  • 既然你做了
    newintegration(),您肯定不会进行依赖项注入,因为不涉及控件容器的反转。尽管现在还不清楚,如果您已经进行了spring引导,并且正确地执行了
    @ImportResource
    ,那么为什么需要
    新的ClassPathXmlApplicationContext(“classpath:META-INF/spring/integration/http outbound config.xml”)

  • 对于Spring集成XML配置,最好的方法是使用
    @ImportResource
    。然后您需要访问
    SpringApplication.run()
    getBean(Integration.class)
    ApplicationContext
    来调用
    start()
    方法。但是,您完全需要忘记
    newintegratio()
    。Spring将为该集成管理一个bean,然后依赖注入将开始工作

  • 异步模式可以通过Spring集成中的执行器通道实现。因此,当您向该通道发送消息时,逻辑将在不同的线程上处理。但是,不清楚您为什么要声明这样的要求,因为您仍然要通过
    replyChannel.receive()
    …进行阻止。。。尽管这应该在单独的SO线程中解决

  • Camel与Spring的集成问题超出了StackOverflow策略

  • 在这个问题的上下文中,Thymeleaf和Bootstrap具有误导性

    请考虑在这里正确地问:


    还可以阅读一些关于依赖注入和控制反转的文档:

    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()); 
        }
    }