SpringBoot集成Netty时在Handler类中注入service和dao为null

最近在做一个服务器接收客户端消息,之前一直都只接触web开发,第一次接触服务器开发,接触到Netty,但在Netty的Handler类里注入为null。

public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Autowired
    private UserRepository userRepository;
    ....
}

userRepository注入后为nul,在上寻求了多种方法,参考了一些大神的博文,Spring Boot非controller使用@Autowired注解注入为null的问题(加@Component注解,再写个初始化函数),依旧无效。

最后,参考https://www.cnblogs.com/s648667069/p/6489557.html的方法 
写了个SpringUtil工具类,代码如下:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {

     private static ApplicationContext applicationContext;

        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            if(SpringUtil.applicationContext == null) {
                SpringUtil.applicationContext = applicationContext;
            }
        }

        //获取applicationContext
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }

        //通过name获取 Bean.
        public static Object getBean(String name){
            return getApplicationContext().getBean(name);
        }

        //通过class获取Bean.
        public static <T> T getBean(Class<T> clazz){
            return getApplicationContext().getBean(clazz);
        }

        //通过name,以及Clazz返回指定的Bean
        public static <T> T getBean(String name,Class<T> clazz){
            return getApplicationContext().getBean(name, clazz);
        }

}
  • 改为:
public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    private static UserRepository userRepository;
    static {
        userRepository = SpringUtil.getBean(UserRepository.class);
    }
    ...
}
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
在Spring Boot中集成Netty可以通过以下步骤完成: 1. 添加Netty依赖:在你的Spring Boot项目的pom.xml文件中添加Netty的依赖。 ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.53.Final</version> </dependency> ``` 2. 创建Netty服务器:创建一个Netty服务器类,该类需要继承自`io.netty.channel.ChannelInboundHandlerAdapter`。 ```java public class NettyServer extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { // 处理接收到的消息 // ... } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // 处理异常情况 // ... } @Override public void channelActive(ChannelHandlerContext ctx) { // 在连接建立执行一些初始化操作 // ... } @Override public void channelInactive(ChannelHandlerContext ctx) { // 在连接关闭执行一些清理操作 // ... } } ``` 3. 配置Netty服务器:在Spring Boot的配置文件中配置Netty服务器的相关参数,例如端口号、线程池等。 ```properties # application.properties netty.server.port=8080 netty.server.workerThreads=10 ``` 4. 启动Netty服务器:在Spring Boot的启动类中初始化并启动Netty服务器。 ```java @SpringBootApplication public class YourApplication { @Autowired private NettyServer nettyServer; public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } @PostConstruct public void startNettyServer() { // 获取Netty服务器配置参数 int port = Integer.parseInt(env.getProperty("netty.server.port")); int workerThreads = Integer.parseInt(env.getProperty("netty.server.workerThreads")); // 创建EventLoopGroup和ServerBootstrap EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads); ServerBootstrap serverBootstrap = new ServerBootstrap(); try { // 设置服务器参数 serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // 添加自定义的Handler pipeline.addLast(nettyServer); } }); // 启动Netty服务器 ChannelFuture channelFuture = serverBootstrap.bind(port).sync(); channelFuture.channel().closeFuture().sync(); } catch (InterruptedException e) { // 处理启动异常 } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } } ``` 这样,你就成功地在Spring Boot项目中集成Netty。你可以在`NettyServer`类中编写自定义的业务逻辑来处理接收到的消息,并在`channelRead`方法中进行处理。同,你也可以根据需要在`exceptionCaught`、`channelActive`和`channelInactive`方法中处理异常、连接建立和连接关闭等事件。记得在服务器关闭进行资源的释放和清理操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值