CDI 拦截器未启动

kry*_*tah 0 java interceptor cdi java-ee-7

我的拦截器没有在应该启动的时候启动,即使它已在 bean 中注册并且文件没有提供任何警告。我缺少什么?

编辑1:我希望能够在每次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>
Run Code Online (Sandbox Code Playgroud)


日志拦截器.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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


日志.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 {
}
Run Code Online (Sandbox Code Playgroud)


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());   
    }   
}   
Run Code Online (Sandbox Code Playgroud)

流派.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 */
}
Run Code Online (Sandbox Code Playgroud)

小智 5

我认为问题是您正在使用的 @SessionScoped 注释。导入表明您正在使用

import javax.faces.bean.SessionScoped;
Run Code Online (Sandbox Code Playgroud)

请切换到CDI:

import javax.enterprise.context.SessionScoped;
Run Code Online (Sandbox Code Playgroud)

另外,您不应该使用 @ManagedBean 注释,请通过 java ee 7 替换它:

@Named
Run Code Online (Sandbox Code Playgroud)