亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot Autowired Repository null

Spring Boot Autowired Repository null

POPMUISE 2023-05-10 17:11:51
我正在使用 spring 框架創建一個 Netty UDP 服務器。我有 3 個類和 1 個接口。UDPServer.javapackage com.example.nettyUDPserver;import java.net.InetAddress;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.stereotype.Component;import akka.actor.ActorRef;import io.netty.bootstrap.Bootstrap;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.ChannelPipeline;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioDatagramChannel;public class UDPServer {    private int port;    ActorRef serverActor = null;    public UDPServer(int port) {        this.port = port;    }    public void run() throws Exception {        final NioEventLoopGroup group = new NioEventLoopGroup();        try {            final Bootstrap b = new Bootstrap();            b.group(group)                .channel(NioDatagramChannel.class)                .option(ChannelOption.SO_BROADCAST, true)                .handler(new ChannelInitializer<NioDatagramChannel>() {                    @Override                    public void initChannel(final NioDatagramChannel ch) throws Exception {                        ChannelPipeline p = ch.pipeline();                        p.addLast(new IncomingPacketHandler());                    }                });            Integer pPort = port;            InetAddress address = InetAddress.getLocalHost();            //InetAddress address = InetAddress.getByName("192.168.1.53");            System.out.println("Localhost address is: " + address.toString());            b.bind(address, pPort).sync().channel().closeFuture().await();        } finally {            group.shutdownGracefully().sync();        }    }    public static void main(String[] args) throws Exception {        int port = 6001;        new UDPServer(port).run();    }}
查看完整描述

2 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

你的類IncomingPacketHandler不是由 Spring 管理的,而是由你親自創建的:

ChannelPipeline p = ch.pipeline();
p.addLast(new IncomingPacketHandler());

因此,即使您添加一百萬個 Spring 注釋,它們也不會執行任何操作。您想要的是讓 Spring 創建此處理程序,并將 Spring 創建的處理程序作為參數傳遞給p.addLast


查看完整回答
反對 回復 2023-05-10
?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

該類IncomingPacketHandler是手動創建的,而不是由 Spring 創建的,因此bean不可用。


添加@Component到IncomingPacketHandler類:


...


import org.springframework.stereotype.Component;


@Component

public class IncomingPacketHandler extends


...

然后在UDPServer.java:


...


import org.springframework.beans.factory.annotation.Autowired;


@Component

public class UDPServer {


@Autowired

private IncomingPacketHandler incomingPacketHandler;

...


查看完整回答
反對 回復 2023-05-10
  • 2 回答
  • 0 關注
  • 289 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號