krystah
krystah

Reputation: 3733

CDI Interceptor not kicking in

My interceptor is not kicking in when it should, even though it's registered in beans and the files provide no warnings. What am I missing?

Edit 1: I want to be able to log each time a function in Genres.java is invoked and left, but I am not getting any output at all with this configuration.

Edit 2: After following Svetlin's advice to apply breakpoints, I can confirm the code never reaches neither the Interceptor or the 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 */
}

Upvotes: 0

Views: 1917

Answers (1)

Kescha Skywalker
Kescha Skywalker

Reputation: 489

I think the problem is the @SessionScoped annotation you are using. The import indicates that you are using

import javax.faces.bean.SessionScoped;

Please switch to the CDI one:

import javax.enterprise.context.SessionScoped;

Also you shouldn't use @ManagedBean annotation, please replace it through the java ee 7 one:

@Named

Upvotes: 5

Related Questions