2017-05-18 6 views
0

春の起動アプリケーションがあります。そして、私はUserCreateEventのようなイベントを作成し、リスナーUserCreateListenerspring boot guava eventbus listenerの登録を解除するには?

イベントがあります。

public class UserCreateEvent { 
    private Long userId; 
} 

がリスナー:

@Component 
public class UserCreateListener { 
    @Autowired 
    private Eventbus eventbus; 

    @PostConstruct 
    public void init() { 
     this.eventbus.register(this) 
    } 

    @Subscribe 
    public void onUserCreate(UserCreateEvent event) { 
     Long userId = event.getUserId(); 
     // todo something necessary 
    } 

} 


@SpringBootApplication 
public class Application { 
    public static void main(String[] args) { 
     SpringApplication application = new SpringApplication(Application.class); 
     application.run(args); 
    } 
} 

を今、私は春のブートアプリケーションの起動後にUserCreateListenerの登録を解除したいです。どうすればEventbusはこのイベントとリスナーを登録解除できますか?

+0

タイプミス: '@ Comonent' =>' Component' @、あなたのコードを更新することができてくださいFranç[email protected] –

+0

OK、ありがとう。 –

+0

作成したばかりのコンポーネントを登録解除する理由を教えてください。あなたがそれを必要としないならば、単に@Componentアノテーションを削除するか、org.springframework.context.annotation.ComponentScan.Filterアノテーションを使用して、SpringブートコンテキストからUserCreateListenerを除外してください。 – db80

答えて

0

あなたがUserCreateListener(またはアプリケーションのコンテキストからBeanを取得する)autowired取得登録を解除したい場合は、UserCreateListener

@Component 
public class UserCreateListener { 
    @Autowired 
    private Eventbus eventbus; 

    @PostConstruct 
    public void init() { 
     this.eventbus.register(this) 
    } 

    public void unregister() { 
     this.eventbus.unregister(this) 
    } 
} 

unregister()方法を紹介してlistenerInstance.unregister()

UPDATE

があなたを作成呼び出しますそこに登録を解除してください。リスナーとイベントバスはシングルトンだと思います。

@Component 
public class MyUnregisterService { 
    @Autowired 
    private Eventbus eventbus; 
    @Autowired 
    private UserCreateListener listener; 

    public void unregister() { 
      eventbus.unregister(listener) 
    } 
} 
+0

リスナーはサードパーティライブラリにありますが、何も変更できません。私はアプリケーションのコンテキストでそれを登録解除したい。 –

+0

回答が更新されました – StanislavL

関連する問題