gpt4 book ai didi

java - Spring Boot Autowiring 存储库 null

转载 作者:行者123 更新时间:2023-11-29 10:08:31 25 4
gpt4 key购买 nike

<分区>

我正在使用 spring 框架创建一个 Netty UDP 服务器。我有 3 个类和 1 个接口(interface)。

UDPServer.java

package 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();
}
}

IncomingPacketHandler.java

package com.example.nettyUDPserver;

import java.net.InetAddress;
import java.nio.charset.StandardCharsets;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import com.example.dao.SensorRepository;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;

@ComponentScan("com.example.dao")
public class IncomingPacketHandler extends SimpleChannelInboundHandler<DatagramPacket> {

@Autowired
SensorRepository repo;

IncomingPacketHandler(){

}

@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
final InetAddress srcAddr = packet.sender().getAddress();

ByteBuf buffer = packet.content();
packet.replace(buffer);
int len = buffer.readableBytes();
byte[] message = new byte[len];
buffer.readBytes(message);
String str = new String(message, StandardCharsets.UTF_8);

ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(str);

int id = actualObj.get("sensor_id").asInt();
String status = actualObj.get("status").asText();

System.out.println("==========================================================");
System.out.println("Source address of datagram received: " + srcAddr.toString());
System.out.println("String message received: " + str);
show();
}

public void show() {
System.out.println("In show function, we will perform our CRUD operations");
System.out.println(repo);
// try {
// this.repo.findAll().forEach(x -> System.out.println(x));
// } catch (NullPointerException e) {
// e.printStackTrace();
// }

}

}

传感器.java

package com.example.models;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Sensor {

@Id
private int sensor_id;
private String status;
private double batLev;

public int getSensor_id() {
return sensor_id;
}

public void setSensor_id(int sensor_id) {
this.sensor_id = sensor_id;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public double getBatLev() {
return batLev;
}

public void setBatLev(double batLev) {
this.batLev = batLev;
}


}

SensorRepository.java

package com.example.dao;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.example.models.Sensor;

@Repository
public interface SensorRepository extends CrudRepository<Sensor, Integer> {

}

我在 UDPServer.java 类中运行我的服务器,我可以成功地获取和解码数据报。问题出在 IncomingPacketHandler.java 类中的 SensorRepository。我在变量中使用了 @Autowired 符号,在界面中使用了 @Repository 注释,但是当我打印 Autowiring 存储库的值时,它是空的,所以我无法进行 SQL 查询。有什么想法吗?

更新

谢谢大家的回答,非常感谢。我将 IncomingPacketHandler 类表示为组件,并在 UDPServer 类中 Autowiring 它。当我运行它时,我得到这个:

[nioEventLoopGroup-2-1] DEBUG io.netty.channel.DefaultChannelPipeline - Discarded inbound message DatagramPacket(/192.168.61.64:59905 => /192.168.61.64:6001, PooledUnsafeDirectByteBuf(ridx: 0, widx: 38, cap: 2048)) that reached at the tail of the pipeline. Please check your pipeline configuration.

这可能超出了这个问题的范围,但你也许可以告诉我方向。再次感谢您。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com