Java CDI拦截器未启动
我的拦截器没有在应该的时候启动,即使它在bean中注册并且文件没有提供任何警告。我错过了什么 Edit 1:我希望每次调用并离开Java CDI拦截器未启动,java,cdi,interceptor,java-ee-7,Java,Cdi,Interceptor,Java Ee 7,我的拦截器没有在应该的时候启动,即使它在bean中注册并且文件没有提供任何警告。我错过了什么 Edit 1:我希望每次调用并离开Genres.java中的函数时,都能记录日志,但这种配置根本没有任何输出 编辑2:遵循Svetlin的建议应用断点后,我可以确认代码不会到达拦截器或生产者 beans.xml <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XML
Genres.java
中的函数时,都能记录日志,但这种配置根本没有任何输出
编辑2:遵循Svetlin的建议应用断点后,我可以确认代码不会到达拦截器或生产者
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>
Log.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());
}
}
}
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 {
}
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());
}
}
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 */
}
LoggerProducer.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());
}
}
}
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 {
}
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());
}
}
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 */
}
Genres.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());
}
}
}
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 {
}
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());
}
}
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 */
}
我认为问题在于您使用的@SessionScoped注释。导入表示您正在使用
import javax.faces.bean.SessionScoped;
请切换到CDI选项:
import javax.enterprise.context.SessionScoped;
另外,您不应该使用@ManagedBean注释,请通过java ee 7替换它:
@Named
我认为问题在于您使用的@SessionScoped注释。导入表示您正在使用
import javax.faces.bean.SessionScoped;
请切换到CDI选项:
import javax.enterprise.context.SessionScoped;
另外,您不应该使用@ManagedBean注释,请通过java ee 7替换它:
@Named
您确定没有调用拦截器吗?您的记录器可能被设置为比您正在记录的更高的日志记录级别(例如,记录器被设置为ERROR,但您正在使用level DEBUG->进行日志记录,因此日志中不会出现任何结果)。请在
@AroundInvoke
方法上设置一个断点,并检查它是否被调用。嗨,Svetlin,谢谢你的回复。我将日志记录到信息级别,因为当我手动创建日志对象并使用它们时,信息级别起作用;所有输出都达到标准输出。我知道如何设置断点,但我不知道如何在web应用程序中“到达”断点。您必须在调试模式下运行应用程序。通常这涉及到重新启动应用程序服务器。在eclipse中,您可以右键单击服务器并单击“在调试模式下重新启动”选项。不使用断点,您可以记录错误,只是为了查看输出是否会到达标准输出。但无论哪种方式,最好检查是否调用了拦截器方法。好的,断点正在工作,但没有调用拦截器:/您可以尝试将记录器插入Genres
类而不是拦截器。还有,您正在使用哪个应用程序服务器?您确定没有调用拦截器吗?您的记录器可能被设置为比您正在记录的更高的日志记录级别(例如,记录器被设置为ERROR,但您正在使用level DEBUG->进行日志记录,因此日志中不会出现任何结果)。请在@AroundInvoke
方法上设置一个断点,并检查它是否被调用。嗨,Svetlin,谢谢你的回复。我将日志记录到信息级别,因为当我手动创建日志对象并使用它们时,信息级别起作用;所有输出都达到标准输出。我知道如何设置断点,但我不知道如何在web应用程序中“到达”断点。您必须在调试模式下运行应用程序。通常这涉及到重新启动应用程序服务器。在eclipse中,您可以右键单击服务器并单击“在调试模式下重新启动”选项。不使用断点,您可以记录错误,只是为了查看输出是否会到达标准输出。但无论哪种方式,最好检查是否调用了拦截器方法。好的,断点正在工作,但没有调用拦截器:/您可以尝试将记录器插入Genres
类而不是拦截器。还有,您正在使用哪个应用程序服务器?确实是这样!非常感谢你:真的是这样!非常感谢你:D