我的拦截器应该在它应该启动时,即使它已在bean中注册并且文件没有提供警告。我错过了什么?
编辑1:我希望每次调用Genres.java
中的函数时都能记录并保留,但是我没有使用此配置获得任何输出。
编辑2:按照Svetlin建议应用断点后,我可以确认代码永远不会到达Interceptor或Producer。
的beans.xml
<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>no.krystah.log.LoggingInterceptor</class>
</interceptors>
</beans>
的 LoggingInterceptor.java
package no.krystah.log;
import javax.inject.Inject;
import javax.interceptor.AroundConstruct;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import org.slf4j.Logger;
@Log @Interceptor
public class LoggingInterceptor {
@Inject
Logger logger;
@AroundConstruct
private void init(InvocationContext ic) throws Exception {
logger.info("Entering constructor");
try {
ic.proceed();
} finally {
logger.info("Exiting constructor");
}
}
@AroundInvoke
public Object logMethod(InvocationContext ic) throws Exception {
logger.info(ic.getTarget().toString()+" - "+ ic.getMethod().getName());
try {
return ic.proceed();
} finally {
logger.info(ic.getTarget().toString()+ " - "+ ic.getMethod().getName());
}
}
}
的 Log.java
package no.krystah.log;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Log {
}
的 LoggerProducer.java
package no.krystah.log;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.bean.SessionScoped;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SessionScoped
public class LoggerProducer {
@Produces
public Logger produceLogger(InjectionPoint injectionPoint) {
return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
Genres.java
package no.krystah;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import no.krystah.entity.Genre;
import no.krystah.log.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Log
@ManagedBean
@SessionScoped
public class Genres {
public Genres() { }
/* Functions */
}
答案 0 :(得分:4)
我认为问题是你正在使用的@SessionScoped注释。导入表明您正在使用
import javax.faces.bean.SessionScoped;
请切换到CDI:
import javax.enterprise.context.SessionScoped;
另外你不应该使用@ManagedBean注释,请通过java ee 7 one替换它:
@Named