2012-06-12 41 views
2

我们可以在不同的ejb-jar中的ejb-jar中使用基于注解的拦截器吗? 我使用@Logged示例尝试了它,但用它进行了删除。有人可以帮我吗?CDI - 使用库中的拦截器类

在core.jar添加:

@Inherited 
@InterceptorBinding 
@Retention(RUNTIME) 
@Target({METHOD, TYPE}) 
public @interface Logged {} 

@Logged 
@Interceptor 
public class LoggedInterceptor implements Serializable { 

    private static final long serialVersionUID = 1L; 

    public LoggedInterceptor() { 
    } 

    @AroundInvoke 
    public Object logMethodEntry(InvocationContext invocationContext) 
      throws Exception { 
     System.out.println("Entering method: " 
      + invocationContext.getMethod().getName() + " in class " 
      + invocationContext.getMethod().getDeclaringClass().getName()); 

     return invocationContext.proceed(); 
    } 
} 

的问题是:如何使用从另一个EJB-JAR这个拦截器(企业应用程序中)?例如:伐木业务方法调用,其中的方法可以在不同的模块中找到:

module1.jar:

public class ModuleClass{ 
    @Logged public void doSomething(){...} 
} 

我试图把<拦截> <类.....豆类.xml,但它不适合我。

感谢您的任何建议!

回答

2

这应该肯定的工作,即使我记得,我摆弄了不少在JBoss 6

你必须激活在它定义的JAR的beans.xml拦截器,我认为有是EAR部署的一个问题,但这是不久前的事情,我无法再访问源代码。

如果这不起作用 - 请在两个JAR中的beans.xml中激活。如果拦截器已注册,则尝试查询BeanManager

+0

感谢您的帮助,但我在Glassfish的JIRA上发现,这是一个“功能”,只会在v4(v3.2被跳过)中修复。 http://java.net/jira/browse/GLASSFISH-15119 – user1451106

+0

感谢您发布此更新 –

1

我和JBoss 7上的我的logging interceptor有完全相同的问题,并修复了覆盖完整拦截器的jar到应用程序中。

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.4</version> 
      <configuration> 
       <overlays> 
        <overlay> 
         <groupId>com.github.t1</groupId> 
         <artifactId>logging-interceptor</artifactId> 
         <type>jar</type> 
         <targetPath>WEB-INF/classes</targetPath> 
        </overlay> 
       </overlays> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

<dependencies> 
    <dependency> 
     <groupId>com.github.t1</groupId> 
     <artifactId>logging-interceptor</artifactId> 
     <version>1.1</version> 
     <optional>true</optional> 
    </dependency> 
</dependencies> 

您仍然必须在应用程序的breans.xml中激活拦截器。

不好,但它的工作原理。在Java EE 7中,它通过将拦截器注释为@Priority而无需激活。