[英]Spring Boot Application does not create .jar file
我正在开发一个 Spring boot.application,它创建了一个使用 Netty 实现的 TCP 服务器。 我已经创建了应用程序并对其进行了测试,一切正常。
The problem is when I want to build a jar from the source, the jar is not created.TCP server starts and responds to client requests but jar is not created in target folder.
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 文件或代码的问题,而是这个 class 的问题。 有人可以指出如何解决这个问题吗?
这是我的 TcpServer class。
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();
}
}
}
You can not Autowire below class because before autowiring any class it should be the part of those class which follow stereotype concept so create another class with @Component, @Servive...etc as per your logic and then autowire within this class.
@Autowired
TcpServer tcpServer;
您能否尝试将此 class 强加在除主要 class 之外的其他地方并尝试创建 jar。
而不是自动装配获取应用程序上下文,以便像这样创建 bean
ConfigurableApplicationContext applicationContext =
SpringApplication.run(OtpServiceApplication.class, args);
TcpSrever tcpServer = applicationContext.getBean(TcpServer.class);
tcpServer.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.