使用自动装配时,抽象 class 对象的子 classes 为空

subclasses of abstract class object is null when using autowired

我有这些类。当我调试时,我看到 spring 在构造函数中创建了服务对象并在两个 类 中调用了构造函数,但是当我想使用这些字段时,它们为空。有什么问题?! (type1Processor、type2Processor 和 type3Processor 为空)

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

这是它的子类

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

这是它的用法:

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

您在摘要中缺少 @Component class ReceivedDataService

这意味着当您使用创建子类时:

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

无法注入 ReceivedDataService 并获取默认值(null)

您正在实例化 ReactiveSocketServerApplication 自己。

因此 spring 无法注入 @Autowired 带注释的 bean,因为该实例是在其生命周期之外创建的。

完全删除:

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

并用 @PostConstruct 注释您的 Start():

@PostConstruct
public void Start() { ... }