Java 使用autowired时,抽象类对象的子类为null

Java 使用autowired时,抽象类对象的子类为null,java,spring,spring-boot,nullpointerexception,autowired,Java,Spring,Spring Boot,Nullpointerexception,Autowired,我有这些课程。当我调试时,我看到spring在构造函数中创建了服务对象,并在这两个类中调用了构造函数,但当我想使用字段时,它们是空的。怎么了?!(type1Processor、type2Processor和type3Processor为空) 这是它的子类 import com.vali.ReactiveSocketServer.model.ReceivedData; import com.vali.ReactiveSocketServer.service.ReceivedDataService;

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

这是它的子类

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);
}
}
import com.vali.ReactiveSocketServer.processor.processor;
导入com.vali.ReactiveSocketServer.processor.Type1Processor;
导入com.vali.ReactiveSocketServer.processor.Type2Processor;
导入com.vali.ReactiveSocketServer.processor.Type3Processor;
导入com.vali.ReactiveSocketServer.receivers.AppServer;
导入com.vali.ReactiveSocketServer.socket.ClientHandler;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入java.util.HashMap;
导入java.util.Map;
@SpringBoot应用程序
公共类ReactiveSocketServerApplication{
私有AppServer-AppServer;
@自动连线
类型1处理器类型1处理器;
@自动连线
类型2处理器类型2处理器;
@自动连线
类型3处理器类型3处理器;
公共静态void main(字符串[]args){
run(ReactiveSocketServerApplication.class,args);
ReactiveSocketServerApplication ReactiveSocketServerApplication=新的ReactiveSocketServerApplication();
reactiveSocketServerApplication.Start();
}
公开作废开始(){
appServer=appServer.getInstance();
Map processorMap=new HashMap();
处理器映射输出(7001,类型1处理器);
处理器映射输出(7002,类型2处理器);
处理器映射输出(7003,类型3处理器);
初始化(processorMap);
新ClientHandler(70011000);
新ClientHandler(70025000);
}
}

抽象类
ReceivedDataService
中缺少
@Component

这意味着当您使用以下方法创建子类时:

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

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

您在抽象类
ReceivedDataService
中缺少
@Component

这意味着当您使用以下方法创建子类时:

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

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

您自己正在实例化
ReactiveSocketServerApplication

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

完全删除此选项:

ReactiveSocketServerApplication reactiveSocketServerApplication = new ReactiveSocketServerApplication();
reactiveSocketServerApplication.Start();
并用
@PostConstruct
注释您的
Start()

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

您正在自己实例化
ReactiveSocketServerApplication

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

完全删除此选项:

ReactiveSocketServerApplication reactiveSocketServerApplication = new ReactiveSocketServerApplication();
reactiveSocketServerApplication.Start();
并用
@PostConstruct
注释您的
Start()

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

当我想把它们放到Map中时,它们是空的,在这一行
processorMap.put(7001,type1Processor)看那里,类似的问题:你能分享上面类的包结构吗?@mario45211谢谢。我只是将
@PostConstruct
添加到我的start方法中,它就开始工作了。谢谢。当我想把它们放到Map中时,它们是空的,在这一行
processorMap.put(7001,type1Processor)看那里,类似的问题:你能分享上面类的包结构吗?@mario45211谢谢。我只是将
@PostConstruct
添加到我的start方法中,它就开始工作了。谢谢。我试过了(有或没有@Component),但它不起作用。你试过在任何子类的构造函数上调试吗?是的。我是在马里奥的帮助下找到的。我应该使用
@PostConstruct
作为我的开始方法抽象类不需要被注释为
组件
,因为Spring不能实例化它们。我尝试了(有或没有@Component)但它不起作用。你尝试过在任何子类的构造函数上调试吗?是的。我是在马里奥的帮助下找到的。我应该在start方法中使用
@PostConstruct
,抽象类不需要注释为
组件,因为Spring无法实例化它们