java - Spring Boot应用程序不创建.jar文件

标签 java maven spring-boot

我正在开发一个 Spring boot.application,它创建一个使用 Netty 实现的 TCP 服务器。我已经创建了该应用程序并对其进行了测试,一切正常。

问题是当我想从源构建一个 jar 时,该 jar 没有创建。TCP 服务器启动并响应客户端请求,但目标文件夹中没有创建 jar。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages={"some.package"})
public class OtpServiceApplication implements ApplicationRunner
{
    @Autowired
    TcpServer tcpServer;

    @Override
    public void run(ApplicationArguments args) throws Exception
    {
        tcpServer.start();
    }

    public static void main(String[] args)
    {
        SpringApplication.run(OtpServiceApplication.class, args);
    }
}

但是,当我删除 TCP 相关代码时,jar 已成功创建。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages={"some.package"})
public class OtpServiceApplication 
{
//    @Autowired
//    TcpServer tcpServer;
//
//    @Override
//    public void run(ApplicationArguments args) throws Exception
//    {
//        tcpServer.start();
//    }

    public static void main(String[] args)
    {
        SpringApplication.run(OtpServiceApplication.class, args);
    }
}

因此我猜这不是我的 POM 文件或代码的问题,而是这个类的问题。有人可以指出如何解决这个问题吗?

这是我的 TcpServer 类。

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class TcpServer {

    @Autowired
    XmlProcessor xmlProcessor;

    @Autowired
    IdleSocketHandler idleSocketHandler;

    @Value("${request.timeout:5000}")
    String requestTimeout;

    @Value("${response.timeout:2000}")
    String responseTimeout;

    @Value("${message-listener.port}")
    String xmlListenerPort;

    @Value("${message-listener.ip}")
    String xmlListenerIp;

    private NioEventLoopGroup connectionAcceptorThreadGroup;
    private NioEventLoopGroup connectionProcessorThreadGroup;
    private ChannelFuture serverSocketFuture;

    @Async
    public void start() throws InterruptedException {

        log.info("Starting XML Listener on [{}:{}]", xmlListenerIp, xmlListenerPort);

        this.connectionAcceptorThreadGroup = new NioEventLoopGroup();
        this.connectionProcessorThreadGroup = new NioEventLoopGroup();

        final int resTimeout = Integer.valueOf(responseTimeout);

        final int reqTimeout = Math.min(Integer.valueOf(requestTimeout), (int) (resTimeout * 1.8));

        try {
            ServerBootstrap b = new ServerBootstrap();

            b.group(connectionAcceptorThreadGroup, connectionProcessorThreadGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast("requestHandler", xmlProcessor);
                            ch.pipeline().addLast("idleStateHandler",
                                    new IdleStateHandler(reqTimeout, resTimeout, 0, TimeUnit.MILLISECONDS));
                            ch.pipeline().addLast("idleSocketHandler", idleSocketHandler);
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .option(ChannelOption.SO_REUSEADDR,true)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            serverSocketFuture = b.bind(xmlListenerIp, Integer.valueOf(xmlListenerPort)).sync();
            log.info("Server listening for transactions on {}:{}", xmlListenerIp, xmlListenerPort);
            serverSocketFuture.channel().closeFuture().sync();
        } finally {
            connectionProcessorThreadGroup.shutdownGracefully();
            connectionAcceptorThreadGroup.shutdownGracefully();
        }
    }
}

最佳答案

您不能在类下面 Autowiring ,因为在 Autowiring 任何类之前,它应该是遵循构造型概念的类的一部分,因此根据您的逻辑使用 @Component、@Servive...等创建另一个类,然后在该类中 Autowiring .

    @Autowired
    TcpServer tcpServer;

你可以尝试将这个类强加到除主类之外的其他地方,并尝试创建一个jar。

关于java - Spring Boot应用程序不创建.jar文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58092855/

相关文章:

java - Spring 3.0——无法为 XML 模式 namespace 上下文找到 Spring NamespaceHandler

rest - 如何将 MediaType 添加到 MappingJackson2HttpMessageConverter 而不是 RestTemplate

java - 使用 Spring WebClient 发出多个请求

java - 为什么maven无法解析父版本中的表达式?

java - 如何在 Java 中中断/停止工作线程(非当前线程)

java - 使用 JCufft 进行实数到复数 FFT

java - 如何访问 WAR 的根目录?

java - 重写 getColumnClass 方法后,jTable 仍然错误地对数字进行排序

java - 在 tomcat 上使用 jamon.war

java - fluidwait<webdriver> 类型中的 withtimeout(duration) 方法不适用于参数 (int, timeunit)