我能够从 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。
小服务程序类:
@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 ===================");
}