Spring 引导和 Spring 集成。问题

Spring Boot and Spring Integration. Problems

我有一个Spring启动应用程序。我有一个专用服务器,我将通过 HTTP GET 请求读取数据。我为 Spring 集成模块配置了 http-outbound-config.xml 文件。 当我运行下面的代码时,一切都很好:

http-出站-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>

主要应用程序Class:

package test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import ru.eco.products.waste.egr.Integration;

@SpringBootApplication
@ImportResource("/META-INF/spring/integration/http-outbound-config.xml")
public class Application {
    public static void main(String[] args) {
        Integration integration = new Integration();
        integration.start();
        SpringApplication.run(WasteWebClientApplication.class,
                              args
                             );
    }
    
}

积分class:

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

但是,当我尝试自动装配 MessageChannel 和 PollableChannel 时,我收到空指针异常。

集成class(无效示例):

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

  1. 问题 1:为什么自动装配不起作用?
  2. 问题2:从专用服务器获取数据并保存到数据库的最佳方式是什么?这样的配置可以吗?我将为响应创建一个模型 class,然后通过 JPA 将其保存到数据库中。
  3. 问题 3:我需要在异步模式下从服务器读取数据。如何在 Spring Boot 中实现它?所以这里的主要想法是我将从 UI 接收一个 POST 方法,并将启动与网络服务的集成。集成完成后,我需要通知用户。
  4. 问题 4:也许 Camel 是最好的解决方案?

堆栈:Java 11,Spring 引导,百里香 + bootstrap。

提前感谢您的回答。

  1. 既然你做了new Integration();,你肯定不会有依赖注入,因为不涉及控制容器的反转。虽然完全不清楚为什么你需要那个 new ClassPathXmlApplicationContext("classpath:META-INF/spring/integration/http-outbound-config.xml") 如果你已经做了 Spring 引导和正确的 @ImportResource.

  2. 最好的方法是您已经为 Spring 集成 XML 配置使用 @ImportResource。然后您需要访问 SpringApplication.run()getBean(Integration.class)ApplicationContext 来调用您的 start() 方法。但是,您完全需要忘记 new Integratio()。 Spring 将为 Integration 管理一个 bean,然后依赖注入将起作用。

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

  4. Camel VS Spring 集成问题不在 Whosebug 政策范围内。

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

请考虑学习如何在此处正确提问:https://whosebug.com/help/how-to-ask

另请阅读一些有关依赖注入和控制反转的文档:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core