Jakarta ee @AroundInvoke拦截器在哪里被调用?
我正在用java做一些测试示例,我提出了一个使用@AroundInvoke的示例。问题是我不知道该方法在哪里被调用 这个测试在调用post()方法的地方进行调用,但我不知道它是如何工作的() 调用顺序是BoundaryLogger,然后是MonitorSink BoundaryLogger.javaJakarta ee @AroundInvoke拦截器在哪里被调用?,jakarta-ee,interceptor,Jakarta Ee,Interceptor,我正在用java做一些测试示例,我提出了一个使用@AroundInvoke的示例。问题是我不知道该方法在哪里被调用 这个测试在调用post()方法的地方进行调用,但我不知道它是如何工作的() 调用顺序是BoundaryLogger,然后是MonitorSink BoundaryLogger.java ... public class BoundaryLogger { @Inject Event<CallEvent> monitoring; @AroundI
...
public class BoundaryLogger {
@Inject
Event<CallEvent> monitoring;
@AroundInvoke
public Object logCall(InvocationContext ic) throws Exception {
long start = System.currentTimeMillis();
try {
return ic.proceed();
} finally {
long duration = System.currentTimeMillis() - start;
monitoring.fire(new CallEvent(ic.getMethod().getName(), duration));
}
}
}
@Stateless
@Interceptors(BoundaryLogger.class)
public class ToDoManager {
@PersistenceContext
EntityManager em;
public ToDo findById(long id) {
return this.em.find(ToDo.class,id);
}
public void delete(long id) {
try {
ToDo reference = this.em.getReference(ToDo.class, id);
this.em.remove(reference);
} catch (EntityNotFoundException e) {
//we want to remove it...
}
}
public List<ToDo> all() {
return this.em.createNamedQuery(ToDo.findAll, ToDo.class).getResultList();
}
public ToDo save(ToDo todo) {
return this.em.merge(todo);
}
public ToDo updateStatus(long id, boolean done) {
ToDo todo = this.findById(id);
if(todo == null){
return null;
}
todo.setDone(done);
return todo;
}
}
我通过做另一个拦截器的例子发现了这一点 @AroundInvoke只定义一个拦截器,它将被具有@interceptor(name_class.class)的类调用 在我的例子中,这是我在查看时丢失的代码 ToDoManager.java
...
public class BoundaryLogger {
@Inject
Event<CallEvent> monitoring;
@AroundInvoke
public Object logCall(InvocationContext ic) throws Exception {
long start = System.currentTimeMillis();
try {
return ic.proceed();
} finally {
long duration = System.currentTimeMillis() - start;
monitoring.fire(new CallEvent(ic.getMethod().getName(), duration));
}
}
}
@Stateless
@Interceptors(BoundaryLogger.class)
public class ToDoManager {
@PersistenceContext
EntityManager em;
public ToDo findById(long id) {
return this.em.find(ToDo.class,id);
}
public void delete(long id) {
try {
ToDo reference = this.em.getReference(ToDo.class, id);
this.em.remove(reference);
} catch (EntityNotFoundException e) {
//we want to remove it...
}
}
public List<ToDo> all() {
return this.em.createNamedQuery(ToDo.findAll, ToDo.class).getResultList();
}
public ToDo save(ToDo todo) {
return this.em.merge(todo);
}
public ToDo updateStatus(long id, boolean done) {
ToDo todo = this.findById(id);
if(todo == null){
return null;
}
todo.setDone(done);
return todo;
}
}
@无状态
@拦截器(BoundaryLogger.class)
公共类ToDomainManager{
@持久上下文
实体管理器;
公共ToDo findById(长id){
返回这个.em.find(ToDo.class,id);
}
公共无效删除(长id){
试一试{
ToDo reference=this.em.getReference(ToDo.class,id);
本.em.remove(参考);
}捕获(EntityNotFounde异常){
//我们想删除它。。。
}
}
公共列表全部(){
返回此.em.createNamedQuery(ToDo.findAll,ToDo.class).getResultList();
}
公共待办事项保存(待办事项){
返回此.em.merge(todo);
}
公共ToDo updateStatus(长id,布尔完成){
ToDo ToDo=this.findById(id);
如果(todo==null){
返回null;
}
todo.setDone(完成);
返回待办事项;
}
}
@AroundInvoke注释用于为托管对象方法指定拦截器方法
我希望,这可以帮助别人