我正在开发一个项目,我正在尝试将依赖项注入类中,但@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) {
}
}
}
}
}
班级 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 :(得分:0)
您的ApplicationServer
课程中缺少@ComponentScan。
@SpringBootApplication
@ComponentScan(basePackages = {"project.serverSide"})
public class ApplicationServer {
}
从static
删除AppointmentService
:
@Autowired
private AppointmentService appointments;
如果您需要静态实例变量,请改用setter注入,如下所示:
private static AppointmentService appointments;
@Autowired
public void setAppointments(AppointmentService appointments){
ApplicationServer.appointments = appointments;
}
答案 1 :(得分:-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" />
并为自动装配添加以下代码: -
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />