0

我能够从 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 ===================");
}
4

1 回答 1

1

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

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

于 2016-06-09T19:22:46.927 回答