除main()

时间:2016-06-09 19:16:49

标签: java spring-boot spring-data-jpa autowired alexa-skills-kit

我能够从main()内部获取存储库功能,但不能在其他任何地方工作。

从我读过的所有内容来看,这似乎与@Autowired成员有关,我尝试使用在Spring" context"之外创建的内容。或"容器。

其他人似乎提出的典型罪魁祸首与使用" 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 ===================");
}

1 个答案:

答案 0 :(得分:1)

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

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