CDI: Using Interceptors across different modules / bean archives

前端 未结 6 494
[愿得一人]
[愿得一人] 2021-02-05 11:18

My Java EE 6 application consists of a war and an ejb module packaged in ear file. I\'m using CDI for DI (i.e. I have a beans.xml file in both modules). I want to use a logging

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 12:18

    If you have no control over the external dependency and you still want to enable interceptors without beans.xml you can write a CDI extension:

    package my.package;
    
    import javax.enterprise.event.Observes;
    import javax.enterprise.inject.spi.AfterTypeDiscovery;
    import javax.enterprise.inject.spi.Extension;
    
    public class MyCdiExtension implements Extension {
    
        public void observeAfterTypeDiscovery(@Observes AfterTypeDiscovery afterTypeDiscovery) {
            afterTypeDiscovery.getInterceptors().add(SomeExternalInterceptor.class);
        }
    }
    

    Add file resources/META-INF/services/javax.enterprise.inject.spi.Extension with content:

    my.package.MyCdiExtension
    

提交回复
热议问题