0

main() 内からリポジトリ機能を動作させることはできますが、それ以外の場所では動作しません。

私が読んだすべてから、これは、Spring の「コンテキスト」または「コンテナー」の外で作成されて使用しようとしている @Autowired メンバーと関係があるようです。

他の人が思いつく典型的な犯人は、「new」を使用してSpringの範囲外になるオブジェクトを作成することに関係していますが、 @Autowired である何かに依存するものすべてが作成されるようにするためにできる限りのことをしましたSpring自体によって。

最初の 10 個の名前を出力するために、main() メソッド内に次の単純な oneOff() メソッドがあり、実行すると期待どおりに動作します。

@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 ===================

ただし、サーブレット クラスに貼り付けたばかりの複製メソッドを呼び出すと、リポジトリがインスタンス化されず、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しましたが、Spring 管理の Bean にはなりません。実際、サーブレット コンテナー (Tomcat など) は、サーブレット クラスのインスタンスをインスタンス化して管理しますが、Spring については何も認識していません。そのため、サーブレットは Spring Bean として作成されないため、オートワイヤーは機能しません。

最善の解決策は、独自のサーブレットを作成する代わりに、 Spring Web MVCを使用してクラスを作成することです。@Controllerそれができない場合はWebApplicationContextUtils、サーブレット内から Spring アプリケーション コンテキストを検索するために使用できます。この場合、Spring Bean を検索できます (サーブレットで自動配線する代わりに)。

于 2016-06-09T19:22:46.927 に答える