2016-07-20 6 views
2

私のEventListener注釈はSpringイベントを受け取りません。ここに私のコードは次のとおりです。SpringBoot EventListenerはイベントを受け取りません

@Component 
public class ProxyConfig { 

    public ProxyConfig() { 
     System.out.println("I can see this in the console"); 
    } 

    @EventListener 
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { 
     System.out.println("WON'T WORK :-("); // FIXME 
    } 

    @EventListener 
    public void test(ApplicationStartedEvent event) { 
     System.out.println("WON'T WORK :-("); // FIXME 
    } 
} 

そして、ここでは私のApplicationクラスです:

@SpringBootApplication 
public class Application { 
    public static void main(String[] args) throws Exception { 
     SpringApplication.run(MyApp.class, args); 
    } 
} 

https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2https://solidsoft.wordpress.com/2015/09/29/annotation-driven-event-listeners-in-spring-4-2/によれば、作業をしなければならないが、それはまだ私が「:-(動作しません印刷できません「文字列:(

任意のアイデア?

ありがとう!

+0

'Application'と' ProxyConfig'はどのパッケージにありますか? –

+0

'Application'は' com.mycompany.app'パッケージにあります。 'ProxyConfig'はサブパッケージにあります:' com.mycompany.app.configuration.impl' – user6479676

+0

'META-INF/spring.factories'ファイルを作成することによって(http://docs.spring.io/spring-boot/ docot/current/reference/html/boot-features-spring-application.html#boot-features-application-events-and-listeners)注釈なしでは動作しますが、...では動作しません。 – user6479676

答えて

3

リスンしている2つのイベントは、どちらもアプリケーションのライフサイクルの早い段階で公開されています。

ApplicationStartedEventは、SpringApplicationが開始されるとすぐに、環境またはApplicationContextが利用可能になる前に、ApplicationListenersが登録された後に、できるだけ早く送信されます。

ApplicationEnvironmentPreparedEventが公開されています。「SpringApplicationが起動していて、環境が最初に検査と変更で利用可能になったとき」。

いずれの場合も、アノテーションとアプリケーションコンテキストを介してリスナーが検出されるまでにイベントが早すぎます。ご覧のとおり、spring.factoriesを使用してリスナーを登録できます。あるいは、SpringApplicationのセッターメソッドを使用することもできます。

関連する問題