Java @Autowired依赖项注入为静态字段返回空指针

Java @Autowired依赖项注入为静态字段返回空指针,java,spring,spring-mvc,dependency-injection,autowired,Java,Spring,Spring Mvc,Dependency Injection,Autowired,我正在从事一个项目,在这个项目中,我试图将依赖项注入到一个类中,但由于某种原因,@Autowired注释似乎不起作用 我有一个类ApplicationServer package project.eHealth; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Serve

我正在从事一个项目,在这个项目中,我试图将依赖项注入到一个类中,但由于某种原因,
@Autowired
注释似乎不起作用

我有一个类ApplicationServer

package project.eHealth;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import project.eHealth.bussiness.AppointmentDto;
import project.eHealth.bussiness.AppointmentService;

@SpringBootApplication
@Component
public class ApplicationServer {

    private static final int PORT = 9001;

    private static HashSet<String> names = new HashSet<String>();

    @Autowired
    private static AppointmentService appointments;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ApplicationServer.class, args);
        System.out.println("The chat server is running.");
        ServerSocket listener = new ServerSocket(PORT);
        try {
            while (true) {
                new Handler(listener.accept()).start();
            }
        } finally {
            listener.close();
        }

    }

        private static class Handler extends Thread {

            private String name;
            private Socket socket;
            private BufferedReader in;
            private PrintWriter out;




            /**
             * Constructs a handler thread, squirreling away the socket.
             * All the interesting work is done in the run method.
             */
            public Handler(Socket socket) {
                this.socket = socket;
            }

            /**
             * Services this thread's client by repeatedly requesting a
             * screen name until a unique one has been submitted, then
             * acknowledges the name and registers the output stream for
             * the client in a global set, then repeatedly gets inputs and
             * broadcasts them.
             */
            public void run() {


                try {

                    // Create character streams for the socket.
                    in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                    out = new PrintWriter(socket.getOutputStream(), true);
                    ObjectMapper mapper = new ObjectMapper();


                    // Request a name from this client.  Keep requesting until
                    // a name is submitted that is not already used.  Note that
                    // checking for the existence of a name and adding the name
                    // must be done while locking the set of names.
                    /*while (true) {
                        out.println("SUBMITNAME");
                        name = in.readLine();
                        if (name == null) {
                            return;
                        }
                        synchronized (names) {
                            if (!names.contains(name)) {
                                names.add(name);
                                break;
                            }
                        }
                    }*/

                    // Now that a successful name has been chosen, add the
                    // socket's print writer to the set of all writers so
                    // this client can receive broadcast messages.



                    // Accept messages from this client and broadcast them.
                    // Ignore other clients that cannot be broadcasted to.
                    while (true) {

                        System.out.println("Aici este controlul!");
                        String input = in.readLine();
                        System.out.println(input);

                        Request request = mapper.readValue(input,Request.class);

                        if (request.getCommand().equals("getAllAppointments")){

                        if (appointments == null){
                            System.out.println("appointmentService is null");
                        }
                        else{
                                List<AppointmentDto> appointmentsList = appointments.getAll();
                                Response response = new Response();
                                response.setAction("RETURNED");
                                response.setData(mapper.writeValueAsString(appointmentsList));
                                String respObj = mapper.writeValueAsString(response);
                                out.println(respObj);
                        }   


                        }

                        System.out.println(input);
                        if (input == null) {
                            return;
                        }

                       String[] command = input.split("&");

                       }

                } catch (IOException e) {
                    System.out.println(e);
                } finally {
                    // This client is going down!  Remove its name and its print
                    // writer from the sets, and close its socket.
                    if (name != null) {

                    }
                    if (out != null) {

                    }

                }
            }




    }
    }
package project.serverSide.server.bussiness;



import java.time.LocalDate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import project.serverSide.server.data.Appointment;
import project.serverSide.server.data.AppointmentRepository;


@Component
@Service
public class AppointmentService {

    @Autowired
    AppointmentRepository appointments;

    public AppointmentService(){

    }

    public List<AppointmentDto> getAll(){

        List<Appointment> appointmentsList = appointments.findAll();
        List<AppointmentDto> appointmentsDto = new ArrayList<AppointmentDto>();

        for (Appointment ap : appointmentsList){
            AppointmentDto apDto = new AppointmentDto();
            apDto.setDoctorName(new SimpleStringProperty(ap.getDoctorName()));
            apDto.setPatientName(new SimpleStringProperty(ap.getPatientName()));
            apDto.setDateIssued(new SimpleObjectProperty<LocalDate>(ap.getDateIssued()));
            appointmentsDto.add(apDto);
        }

        return appointmentsDto;
    }
}
package project.eHealth;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.util.HashSet;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.boot.SpringApplication;
导入org.springframework.boot.autoconfigure.springboot应用程序;
导入org.springframework.stereotype.Component;
导入com.fasterxml.jackson.databind.ObjectMapper;
进口项目、电子健康、商务、任命;
进口项目、电子健康、商务、任命服务;
@SpringBoot应用程序
@组成部分
公共类应用服务器{
专用静态最终int端口=9001;
私有静态HashSet name=新HashSet();
@自动连线
私人静态任命服务任命;
公共静态void main(字符串[]args)引发异常{
run(ApplicationServer.class,args);
System.out.println(“聊天服务器正在运行”);
ServerSocket侦听器=新的ServerSocket(端口);
试一试{
while(true){
新处理程序(listener.accept()).start();
}
}最后{
listener.close();
}
}
私有静态类处理程序扩展线程{
私有字符串名称;
专用插座;
中的私有缓冲区读取器;
私人打印输出;
/**
*构造一个处理程序线程,存储套接字。
*所有有趣的工作都是在run方法中完成的。
*/
公共处理程序(套接字){
this.socket=socket;
}
/**
*通过重复请求
*屏幕名称,直到提交了唯一的屏幕名称,然后
*确认该名称并为其注册输出流
*客户端在一个全局集合中,然后重复获取输入和
*广播他们。
*/
公开募捐{
试一试{
//为套接字创建字符流。
in=新的BufferedReader(新的InputStreamReader(
getInputStream());
out=新的PrintWriter(socket.getOutputStream(),true);
ObjectMapper mapper=新的ObjectMapper();
//从该客户端请求名称。继续请求,直到
//提交的名称尚未使用。请注意
//检查名称是否存在并添加名称
//必须在锁定名称集时执行。
/*while(true){
out.println(“提交名称”);
name=in.readLine();
if(name==null){
返回;
}
已同步(名称){
如果(!names.contains(name)){
名称。添加(名称);
打破
}
}
}*/
//现在已经选择了一个成功的名称,添加
//socket的打印写入程序到所有写入程序的集合,因此
//此客户端可以接收广播消息。
//接受来自此客户端的消息并广播它们。
//忽略无法广播到的其他客户端。
while(true){
System.out.println(“Aici este controlul!”);
字符串输入=in.readLine();
系统输出打印项次(输入);
Request-Request=mapper.readValue(输入,Request.class);
if(request.getCommand().equals(“getAllAppPoints”)){
如果(约会==null){
System.out.println(“任命服务为空”);
}
否则{
List AppointsList=Appoints.getAll();
响应=新响应();
响应。设置动作(“返回”);
response.setData(mapper.writeValueAsString(appointmentsList));
String respObj=mapper.writeValueAsString(响应);
out.println(respObj);
}   
}
系统输出打印项次(输入);
如果(输入==null){
返回;
}
String[]命令=input.split(&);
}
}捕获(IOE异常){
系统输出打印ln(e);
}最后{
//此客户端正在关闭!请删除其名称和打印内容
//从集合中选择writer,然后关闭其套接字。
if(name!=null){
}
if(out!=null){
}
}
}
}
}
以及班级任命服务

package project.eHealth;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import project.eHealth.bussiness.AppointmentDto;
import project.eHealth.bussiness.AppointmentService;

@SpringBootApplication
@Component
public class ApplicationServer {

    private static final int PORT = 9001;

    private static HashSet<String> names = new HashSet<String>();

    @Autowired
    private static AppointmentService appointments;

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ApplicationServer.class, args);
        System.out.println("The chat server is running.");
        ServerSocket listener = new ServerSocket(PORT);
        try {
            while (true) {
                new Handler(listener.accept()).start();
            }
        } finally {
            listener.close();
        }

    }

        private static class Handler extends Thread {

            private String name;
            private Socket socket;
            private BufferedReader in;
            private PrintWriter out;




            /**
             * Constructs a handler thread, squirreling away the socket.
             * All the interesting work is done in the run method.
             */
            public Handler(Socket socket) {
                this.socket = socket;
            }

            /**
             * Services this thread's client by repeatedly requesting a
             * screen name until a unique one has been submitted, then
             * acknowledges the name and registers the output stream for
             * the client in a global set, then repeatedly gets inputs and
             * broadcasts them.
             */
            public void run() {


                try {

                    // Create character streams for the socket.
                    in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                    out = new PrintWriter(socket.getOutputStream(), true);
                    ObjectMapper mapper = new ObjectMapper();


                    // Request a name from this client.  Keep requesting until
                    // a name is submitted that is not already used.  Note that
                    // checking for the existence of a name and adding the name
                    // must be done while locking the set of names.
                    /*while (true) {
                        out.println("SUBMITNAME");
                        name = in.readLine();
                        if (name == null) {
                            return;
                        }
                        synchronized (names) {
                            if (!names.contains(name)) {
                                names.add(name);
                                break;
                            }
                        }
                    }*/

                    // Now that a successful name has been chosen, add the
                    // socket's print writer to the set of all writers so
                    // this client can receive broadcast messages.



                    // Accept messages from this client and broadcast them.
                    // Ignore other clients that cannot be broadcasted to.
                    while (true) {

                        System.out.println("Aici este controlul!");
                        String input = in.readLine();
                        System.out.println(input);

                        Request request = mapper.readValue(input,Request.class);

                        if (request.getCommand().equals("getAllAppointments")){

                        if (appointments == null){
                            System.out.println("appointmentService is null");
                        }
                        else{
                                List<AppointmentDto> appointmentsList = appointments.getAll();
                                Response response = new Response();
                                response.setAction("RETURNED");
                                response.setData(mapper.writeValueAsString(appointmentsList));
                                String respObj = mapper.writeValueAsString(response);
                                out.println(respObj);
                        }   


                        }

                        System.out.println(input);
                        if (input == null) {
                            return;
                        }

                       String[] command = input.split("&");

                       }

                } catch (IOException e) {
                    System.out.println(e);
                } finally {
                    // This client is going down!  Remove its name and its print
                    // writer from the sets, and close its socket.
                    if (name != null) {

                    }
                    if (out != null) {

                    }

                }
            }




    }
    }
package project.serverSide.server.bussiness;



import java.time.LocalDate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import project.serverSide.server.data.Appointment;
import project.serverSide.server.data.AppointmentRepository;


@Component
@Service
public class AppointmentService {

    @Autowired
    AppointmentRepository appointments;

    public AppointmentService(){

    }

    public List<AppointmentDto> getAll(){

        List<Appointment> appointmentsList = appointments.findAll();
        List<AppointmentDto> appointmentsDto = new ArrayList<AppointmentDto>();

        for (Appointment ap : appointmentsList){
            AppointmentDto apDto = new AppointmentDto();
            apDto.setDoctorName(new SimpleStringProperty(ap.getDoctorName()));
            apDto.setPatientName(new SimpleStringProperty(ap.getPatientName()));
            apDto.setDateIssued(new SimpleObjectProperty<LocalDate>(ap.getDateIssued()));
            appointmentsDto.add(apDto);
        }

        return appointmentsDto;
    }
}
包project.serverSide.server.business;
导入java.time.LocalDate;
导入java.util.ArrayList;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
进口或
@Autowired
private AppointmentService appointments;
private static AppointmentService appointments;

@Autowired
public void setAppointments(AppointmentService appointments){
    ApplicationServer.appointments = appointments;
}
<context:component-scan annotation-config="true"
    base-package="com.amstech.mayal" />
<context:annotation-config />
<jpa:repositories base-package="com.amstech.mayal.repository" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />