0

I have these classes. when I debug I see that the spring creates the service object in constructor and called the constructors in both classes but when I want to use the fields, they are null. What's the problem?! (type1Processor, type2Processor and type3Processor are null)

import com.vali.ReactiveSocketServer.service.ReceivedDataService;
import org.springframework.stereotype.Component;

public abstract class Processor {

public ReceivedDataService receivedDataService;

public Processor(ReceivedDataService receivedDataService) {
    this.receivedDataService = receivedDataService;
}

public abstract void readStream(String stream);
}

and this is its subclass

import com.vali.ReactiveSocketServer.model.ReceivedData;
import com.vali.ReactiveSocketServer.service.ReceivedDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Type1Processor extends Processor {


@Autowired
public Type1Processor(ReceivedDataService receivedDataService) {
    super(receivedDataService);
}

@Override
public void readStream(String stream) {
    System.out.println("readStream "+ getClass().getSimpleName() + "-" + stream);
    receivedDataService.add(new ReceivedData(stream.getBytes()));
}
}

and this is its usage:

import com.vali.ReactiveSocketServer.processor.Processor;
import com.vali.ReactiveSocketServer.processor.Type1Processor;
import com.vali.ReactiveSocketServer.processor.Type2Processor;
import com.vali.ReactiveSocketServer.processor.Type3Processor;
import com.vali.ReactiveSocketServer.receivers.AppServer;
import com.vali.ReactiveSocketServer.socket.ClientHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class ReactiveSocketServerApplication {

private AppServer appServer;

@Autowired
Type1Processor type1Processor;

@Autowired
Type2Processor type2Processor;

@Autowired
Type3Processor type3Processor;

public static void main(String[] args) {
    SpringApplication.run(ReactiveSocketServerApplication.class, args);

    ReactiveSocketServerApplication reactiveSocketServerApplication = new ReactiveSocketServerApplication();
    reactiveSocketServerApplication.Start();
}


public void Start(){

    appServer = AppServer.getInstance();

    Map<Integer, Processor> processorMap = new HashMap<>();

    processorMap.put(7001, type1Processor);
    processorMap.put(7002, type2Processor);
    processorMap.put(7003, type3Processor);

    appServer.initialize(processorMap);

    new ClientHandler(7001, 1000);
    new ClientHandler(7002, 5000);
}
}
masoud vali
  • 1,528
  • 2
  • 18
  • 29

2 Answers2

3

You are instantiating ReactiveSocketServerApplication yourself.

So spring can't inject the @Autowired annotated beans, because the instance was created outside of it's life cycle.

Remove this completly:

ReactiveSocketServerApplication reactiveSocketServerApplication = new ReactiveSocketServerApplication();
reactiveSocketServerApplication.Start();

And annotate your Start() with @PostConstruct:

@PostConstruct
public void Start() { ... }
Lino
  • 19,604
  • 6
  • 47
  • 65
-1

You are missing @Component in the abstract class ReceivedDataService.

This mean when you create the subclases using :

@Autowired
public Type1Processor(ReceivedDataService receivedDataService) {
   super(receivedDataService);
}

Can't inject ReceivedDataService and get the default value(null)

Bastida
  • 105
  • 1
  • 9