简体   繁体   中英

CDI 1.1: Is @Observes @Initialized(TransactionScoped.class) supposed to work?

CDI 1.1: Is @Observes @Initialized(TransactionScoped.class) supposed to work?

@ApplicationScoped
public class MyClass {
public void processConversationScopedInit(@Observes 
  @Initialized(TransactionScoped.class) Object payload) {}

  public void processConversationScopedDestroyed(@Observes 
  @Destroyed(TransactionScoped.class) Object payload) {}
}

It's a standard scope in CDI 1.1, but it's conspiciuously missing from this list here:

http://www.next-presso.com/2014/06/you-think-you-know-everything-about-cdi-events-think-again/

I can't seem to get it to work in TomEE 7.0.5/OpenWebBeans, and I'm curious if that's just an oversight in the spec. Thanks!

@TransactionScoped is not a standard scope in a way that it is not implemented by CDI (or, well, CDI impls). It comes from JTA and it also has their package - javax.transaction .

That being said, it should behave as all other @NormalScope contexts and hence should fire the @Initialized events for you to observe. Although do note that CDI spec only recommends this by saying:

Portable extensions are encouraged to fire an event with qualifier @Initialized(X.class) when a custom context is initialized, and an event with qualifier @Destroyed(X.class) when a custom context is destroyed, where X is the scope type associated with the context. A suitable event payload should be chosen.

I haven't tried this myself but I would wager this works for most up to date implementations. Then again, you are in CDI 1.1 which is now pretty ancient.

Yes, since it is not required and potentially adds a significative overhead which can be avoided using the already provided request scope event then this was not added to tomee.

Issue when you start implementing something from javax.* which is not spec-ed is that users will think it is portable when it is actually wrong so better to respect the spec when possible IMHO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM