第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Spring Boot Autowired Repository null

Spring Boot Autowired Repository null

POPMUISE 2023-05-10 17:11:51
我正在使用 spring 框架創(chuàng)建一個 Netty UDP 服務(wù)器。我有 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條經(jīng)驗 獲得超3個贊

你的類IncomingPacketHandler不是由 Spring 管理的,而是由你親自創(chuàng)建的:

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

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


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

TA貢獻1815條經(jīng)驗 獲得超13個贊

該類IncomingPacketHandler是手動創(chuàng)建的,而不是由 Spring 創(chuàng)建的,因此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;

...


查看完整回答
反對 回復(fù) 2023-05-10
  • 2 回答
  • 0 關(guān)注
  • 288 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號