gpt4 book ai didi

java - 除了 main() 之外, Autowiring 的存储库在任何地方都是 null

转载 作者:行者123 更新时间:2023-12-01 09:49:03 25 4
gpt4 key购买 nike

我可以从 main() 内部获取存储库功能,但不能在其他地方使用。

从我读到的所有内容来看,这似乎与我试图使用在 Spring 的“上下文”或“容器”之外创建的 @Autowired 成员有关。

其他人似乎想出的典型罪魁祸首与使用“new”创建一个最终超出 Spring 范围的对象有关,但我已尽我所能来确保所有依赖于@的东西Autowired 是 Spring 自己创建的。

我的 main() 方法中有以下简单的 oneOff() 方法,用于打印前 10 个名称,该方法在运行时按预期工作:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Autowired
private AppointmentServlet appointmentServlet;

@Autowired
private IAppointmentRepository appointmentRepository;

@Autowired
private IStaffMemberRepository staffMemberRepository;

@Autowired
private IStaffMemberRepository clientRepository;

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
Application app = context.getBean(Application.class);
app.oneOff();
}

@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(appointmentServlet, "/AppointmentManager/NextAppointment");
}

private void oneOff() {
System.out.println("\n=================== Starting lookup ===================");
try {

int limit = 10;
for (int i = 0; i < limit; i++) {
StaffMember member = staffMemberRepository.findAll().get(i);
String firstName = member.getFirstName();
String lastName = member.getLastName();
if (firstName == null || firstName.equals("UNKNOWN") || firstName.isEmpty()) {
limit++;
continue;
}
System.out.println("Staff member: " + firstName + " " + lastName);
}
} catch (NullPointerException ex) {
System.out.println("NPE: Lookup failed");
} System.out.println("==================== Ending lookup ===================");
}

}

产生:

=================== Starting lookup ===================
Staff member: JOSE BEJARANO
Staff member: KARIMAH ABDUL-AZIZ
Staff member: CONNIE ABED
Staff member: WALID ABI-SAAB
Staff member: BARRY ABRAHAM
Staff member: JENNA ACEYCEK
Staff member: DANIEL ADAMCZ YK
Staff member: TYMOTEUSZ ADAMCZYK
Staff member: ELIZABETH ADAMS
Staff member: ERIN ADAMS
==================== Ending lookup ===================

但是,当我调用刚刚粘贴到我的 servlet 类中的重复方法时,存储库永远不会被实例化,并且我会得到 NullPointerExceptions。

Servlet 类:

@Component
public class AppointmentServlet extends SpeechletServlet {

@Autowired
private AppointmentSpeechlet appointmentSpeechlet;

public AppointmentServlet() {
this.setSpeechlet(appointmentSpeechlet);
}

}

实际使用存储库的 Speechlet 类:

@Component
public class AppointmentSpeechlet implements Speechlet {

@Autowired
private IAppointmentRepository appointmentRepository;

@Autowired
private IStaffMemberRepository staffMemberRepository;

@Autowired
private IClientRepository clientRepository;

@Override
public SpeechletResponse onIntent(final IntentRequest request, final Session session) throws SpeechletException {
oneOff();
}
}

再次使用oneOff()方法:

private void oneOff() {
System.out.println("\n=================== Starting lookup ===================");
try {

int limit = 10;
for (int i = 0; i < limit; i++) {
StaffMember member = staffMemberRepository.findAll().get(i);
String firstName = member.getFirstName();
String lastName = member.getLastName();
if (firstName == null || firstName.equals("UNKNOWN") || firstName.isEmpty()) {
limit++;
continue;
}
System.out.println("Staff member: " + firstName + " " + lastName);
}
} catch (NullPointerException ex) {
System.out.println("NPE: Lookup failed");
} System.out.println("==================== Ending lookup ===================");
}

最佳答案

您已将 @Component 添加到您的 servlet 类中,但这并不使其成为 Spring 管理的 bean。事实上,servlet 容器(例如 Tomcat)实例化并管理 servlet 类的实例,并且它对 Spring 一无所知 - 因此您的 servlet 不是作为 Spring bean 创建的,因此 Autowiring 不起作用。

最好的解决方案是使用 Spring Web MVC并编写一个 @Controller 类,而不是编写您自己的 servlet。如果您不能这样做,您可以使用 WebApplicationContextUtils从 servlet 中查找 Spring 应用程序上下文,您可以在其中查找 Spring bean(而不是在 servlet 中 Autowiring 它们)。

关于java - 除了 main() 之外, Autowiring 的存储库在任何地方都是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37734435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com