Java 如何在spring boot中创建Tcp连接以接受连接?

Java 如何在spring boot中创建Tcp连接以接受连接?,java,spring-boot,tcp,spring-integration,Java,Spring Boot,Tcp,Spring Integration,我已经了解到,我需要创建一个tcprevivingchanneladapter来接受连接。 但我不知道该怎么做 有人能给我介绍一下吗?有关使用XML配置的一些指针,请参阅 用于Java配置;这里有一个简单的Spring启动应用程序 package com.example; import java.net.Socket; import javax.net.SocketFactory; import org.springframework.boot.SpringApplication; imp

我已经了解到,我需要创建一个
tcprevivingchanneladapter
来接受连接。 但我不知道该怎么做

有人能给我介绍一下吗?

有关使用XML配置的一些指针,请参阅

用于Java配置;这里有一个简单的Spring启动应用程序

package com.example;

import java.net.Socket;

import javax.net.SocketFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory;
import org.springframework.integration.transformer.ObjectToStringTransformer;
import org.springframework.messaging.MessageChannel;

@SpringBootApplication
public class So39290834Application {

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(So39290834Application.class, args);
        Socket socket = SocketFactory.getDefault().createSocket("localhost", 9999);
        socket.getOutputStream().write("foo\r\n".getBytes());
        socket.close();
        Thread.sleep(1000);
        context.close();
    }

    @Bean
    public TcpNetServerConnectionFactory cf() {
        return new TcpNetServerConnectionFactory(9999);
    }

    @Bean
    public TcpReceivingChannelAdapter inbound(AbstractServerConnectionFactory cf) {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setConnectionFactory(cf);
        adapter.setOutputChannel(tcpIn());
        return adapter;
    }

    @Bean
    public MessageChannel tcpIn() {
        return new DirectChannel();
    }

    @Transformer(inputChannel = "tcpIn", outputChannel = "serviceChannel")
    @Bean
    public ObjectToStringTransformer transformer() {
        return new ObjectToStringTransformer();
    }

    @ServiceActivator(inputChannel = "serviceChannel")
    public void service(String in) {
        System.out.println(in);
    }

}

完美的你能告诉我在哪里可以找到关于SpringBoot中基于Java的配置的文档吗?我不知道你的意思。如果您指的是boot中Spring集成组件的java配置,我们在参考手册中有一些示例,但并非所有模块都有。我们会为您提供指导,并告诉您在哪里可以找到有关大多数元素的底层组件的信息。gradle依赖项:compile'org.springframework.integration:spring integration核心:4.3.11.RELEASE'compile'org.springframework.integration:spring integration ip:4.3.11.RELEASE'您的问题不清楚。在任何情况下,您不得在评论中提出新问题;以更详细的方式提出一个新问题,准确地解释你遇到的问题。你在说什么?哪种豆子?定义“将起作用”。连接工厂允许客户端连接;另一个bean是Spring集成基础设施,最终是只打印字符串的服务。除非你有什么有意义的补充,否则不要评论旧答案。如果你有什么不明白的地方,用适当的标签问一个新问题,并准确地表达你的意思。