Spring Boot Autowired Repository null

Orestis Zekai :

I am creating a Netty UDP server using the spring framework. I have 3 classes and 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();
//      }

    }

}

Sensor.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> {

}

I am running my server in the class UDPServer.java and I can successfully get and decode datagrams. The problem is with the SensorRepository in the IncomingPacketHandler.java class. I am using the @Autowired notation in the variable and I am using the @Repository annotation in the interface, but when I print the value of the autowired repository, it is null, so I cannot make SQL queries. Any ideas?

UPDATE

Thank you for your answers guys, much appreciated. I am denoting the IncomingPacketHandler class as a component and I am autowiring it in the UDPServer class. When I run it I get this:

[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.

This is probably out of the scope of this question, but you maybe can show me tha direction. Thank you once again.

Jeroen Steenbeeke :

Your class IncomingPacketHandler is not managed by Spring, but created by you personally:

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

As such, even if you add a million Spring annotations, they won't do anything. What you want instead is to have Spring create this handler, and pass the Spring-created handler as argument to p.addLast

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308441&siteId=1