【问题标题】:CDI - using interceptor class from libraryCDI - 使用库中的拦截器类
【发布时间】:2012-06-15 07:10:27
【问题描述】:

我们可以在来自不同 ejb-jar 的 ejb-jar 中使用基于注释的拦截器吗? 我使用@Logged 示例进行了尝试,但还是坚持了下来。有人可以帮帮我吗?

在 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(){...}
}

我也尝试将

感谢您的建议!

【问题讨论】:

    标签: java logging glassfish cdi interceptor


    【解决方案1】:

    这肯定行得通,尽管我记得我在 JBoss 6 上摆弄了不少。

    您必须在定义它的 JAR 的 beans.xml 中激活拦截器,我认为 EAR 部署存在问题,但那是很久以前的事了,我没有不能再访问源代码了。

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

    【讨论】:

    【解决方案2】:

    我在 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,它无需激活即可工作。

    【讨论】:

    • @Priority 注释解决了我的问题。谢谢:)
    猜你喜欢
    • 2020-06-17
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多