为什么我的拦截器不起作用?
MyLog.java
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface MyLog {
}
MyLogger.java
@Interceptor
@MyLog
@Priority(Interceptor.Priority.APPLICATION)
public class MyLogger {
@AroundInvoke
public Object log(InvocationContext context) throws Exception{
System.out.println("begin " + context.getMethod().getName());
Object obj = context.proceed();
System.out.println("end " + context.getMethod().getName());
return obj;
}
}
PerguntaController.java
import interceptor.MyLog;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
@Named("PerguntaController")
@SessionScoped
public class PerguntaController implements Serializable {
@EJB
private PerguntaFacade ejbFacade;
@MyLog
public List<> getAll() {
return ejbFacade.getAll();
}
@MyLog
public void update(Pergunta pergunta) {
ejbFacade.update(pergunta);
}
}
PerguntaFacade.java
import interceptor.MyLog;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class PerguntaFacade {
@PersistenceContext(unitName = "WebApplicationPU")
private EntityManager em;
@MyLog
public List<Pergunta> getAll() {
return em.createQuery("SELECT p FROM Pergunta p", Pergunta.class).getResultList();
}
@MyLog
public void update(Pergunta pergunta) {
//do something
}
}
在jsf页面中使用getAll和update(来自PerguntaController)时,不会触发拦截器getAll并在PerguntaFacade上更新。我做错了什么?
答案 0 :(得分:0)
解决。
使用bean-discovery-mode="annotated"
的 beans.xml 不起作用。
然后更改为bean-discovery-mode="all"
并正常工作。