gpt4 book ai didi

java - 拦截器的@AroundInvoke未触发

转载 作者:太空宇宙 更新时间:2023-11-04 11:09:20 24 4
gpt4 key购买 nike

我正在研究拦截器在java中是如何工作的。我正在使用 Netbeans IDE,并且刚刚创建了一个名为 Interceptors 的新项目。

我创建了一个名为“logged”的注释

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

然后我创建了一个类“LoggedInterceptor”

@Interceptor
public class LoggedInterceptor implements Serializable {

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();
}
}

然后我刚刚创建了一个使用 Logged 注释的类

public class SuperService 
{
@Logged
public String deliverService(String uid)
{
return uid;
}

public static void main(String[] args)
{
SuperService ss = new SuperService();
System.out.println(ss.deliverService("sisi"));
}
}

什么也没发生。后来我在 src/main/resources/META-INF/下添加了一个名为 beans.xml 的 xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1"
bean-discovery-mode="all">
<interceptors>
<class>ascompany.interceptors.LoggedInterceptor</class>
</interceptors>
</beans>

但是当我调用 DeliverService 方法时,logMethodEntry 方法不会被调用。我是否缺少其他一些配置文件?或者只是别的什么?

我已经尝试向 LoggedInterceptor 添加 @Priority 注释,但没有任何改变......

编辑:

我按照 @Luciano van der Veekens 的说法向 LoggedInterceptor 添加了 logget 注释,但没有任何改变

最佳答案

您忘记用@Logged注释LoggedInterceptor类。这实际上将注释绑定(bind)到拦截器。

@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {
}

他们在Java EE 6之一中做了同样的事情教程。

而且看起来SuperService只是一个普通的类。拦截器是一个 Java EE 概念,仅适用于部署在应用程序服务器上的 EJB。

关于java - 拦截器的@AroundInvoke未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46196529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com