2017-05-20 16 views
0

私はクラスに依存関係を注入しようとしていますが、@Autowired注釈付けは何らかの理由で機能していないようです。@Autowired dependency injectionはstaticフィールドのnullポインタを返します

私は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) { 

        } 

       } 
      } 




    } 
    } 

とクラスAppointmentService

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

そしてAppointmentRepository

package project.serverSide.server.data; 


    import java.util.List; 

    import org.springframework.data.jpa.repository.JpaRepository; 
    import org.springframework.stereotype.Component; 

    @Component 
    public interface AppointmentRepository extends JpaRepository<Appointment,Integer> { 
     List<Appointment> findAll(); 
    } 

問題は、ApplicationServerクラスは何らかの理由でAppointmentServiceを注入しないため、理由がわかりません。私は答えのための何時間もの検索のために重要な数を費やしてきましたが、何の結果もありませんでした。

非常に感謝しています!

答えて

0

ApplicationServerクラスには@ComponentScanがありません。

@SpringBootApplication 
@ComponentScan(basePackages = {"project.serverSide"}) 
public class ApplicationServer { 
} 

そしてAppointmentServiceからstaticを削除する:あなたは、静的インスタンス変数を必要とする代わりに、セッター・インジェクションを使用している場合

@Autowired 
private AppointmentService appointments; 

、次のように:

private static AppointmentService appointments; 

@Autowired 
public void setAppointments(AppointmentService appointments){ 
    ApplicationServer.appointments = appointments; 
} 
+0

動作しません... – Dragos2795

+0

私がいることを言及するforgotenていますアプリケーションが正常に起動しました(ビルド時に例外をスローしません)。 – Dragos2795

+0

@ Dragos2795私の更新された答えをチェックします。 – Arpit

-1

はあなたがApplicationContext.xmlでベースパッケージを追加する必要があります、u =あなたは以下のコードを使用できます: -

<context:component-scan annotation-config="true" 
    base-package="com.amstech.mayal" /> 
追加ベースパッケージのFOTリポジトリのためのコード使用以下

: -

<context:annotation-config /> 
<jpa:repositories base-package="com.amstech.mayal.repository" /> 

ともAutowiredのためのコードの下に追加します -

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> 
関連する問題