fghf
fghf

Reputation: 734

CDI doesn't see an interceptor

I don't understand why interceptor is not found. I get the following error: the class is not found, or not annotated with @Interceptor and still not registered through a portable extension, or not annotated with @Dependent inside an implicit bean archive Interceptor itself:

    @Interceptor
    @Loggable
    public class LoggingInterceptor {

    @Inject
    private Logger logger;

    @AroundConstruct
    public void init(InvocationContext ic) throws Exception {
        logger.fine("Entering constructor");
        try {
            ic.proceed();
        } finally {
            logger.fine("Exiting constructor");
        }
    }

    @AroundInvoke
    public Object logMethod(InvocationContext ic) throws Exception {
        logger.entering(ic.getTarget().toString(),ic.getMethod().getName());
        try {
            return ic.proceed();
        } finally {
            logger.exiting(ic.getTarget().toString(),ic.getMethod().getName());
        }
     }
   }

Here is a beans.xml where it is specified:

 <?xml version="1.0" encoding="UTF-8"?>
    <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"
        bean-discovery-mode="all">
    <alternatives>
        <class>org.abondar.experimental.javaeedemo.basiccdi.test.MockGenerator</class>
    </alternatives>
    <interceptors>
        <class>org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor</class>
    </interceptors>
</beans>

log producer:

public class LoggingProducer {

    @Produces
    private Logger createLogger(InjectionPoint injectionPoint){
        return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }
}

And loggable annotation:

@InterceptorBinding
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {
}

For running the app I am using Weld Se.Here is Main.class

public class Main {
    public static void main(String[] args) {

        Weld weld = new Weld();
        WeldContainer container = weld.initialize();

        BookService service = container.select(BookService.class).get();

        Book book = service.createBook("Salo",5.0f,"A book of salo");
        System.out.println(book);

        weld.shutdown();
    }
}

There is another weird problem which I believe causes the one I am asking about: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.Main because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.Main not found.

I see this message related to all classes in my example.

So an entire stack trace:

Nov 16, 2017 1:24:42 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 3.0.1 (Final)
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.Main because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.Main not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.test.MockGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.test.MockGenerator not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.BookService because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.BookService not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.ThirteenDigits because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.ThirteenDigits not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.IsbnGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.IsbnGenerator not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.NumberGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.NumberGenerator not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.Book because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.Book not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.Loggable because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.Loggable not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.LoggingProducer because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.LoggingProducer not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.IssnGenerator because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.IssnGenerator not found.  If this is unexpected, enable DEBUG logging to see the full error.
Nov 16, 2017 1:24:43 PM org.jboss.weld.bootstrap.MissingDependenciesRegistry handleResourceLoadingException
INFO: WELD-000119: Not generating any bean definitions from org.abondar.experimental.javaeedemo.basiccdi.EightDigits because of underlying class loading error: Type org.abondar.experimental.javaeedemo.basiccdi.EightDigits not found.  If this is unexpected, enable DEBUG logging to see the full error.
[WARNING] 
org.jboss.weld.exceptions.DeploymentException: WELD-001417: Enabled interceptor class org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor (<class>org.abondar.experimental.javaeedemo.basiccdi.LoggingInterceptor</class> in file:/home/abondar/IdeaProjects/JavaEEDemo/BasicCDI/target/classes/META-INF/beans.xml@12) does not match an interceptor bean: the class is not found, or not annotated with @Interceptor and still not registered through a portable extension, or not annotated with @Dependent inside an implicit bean archive
    at org.jboss.weld.bootstrap.Validator.validateEnabledInterceptorClasses(Validator.java:661)
    at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:481)
    at org.jboss.weld.bootstrap.WeldStartup.validateBeans(WeldStartup.java:480)
    at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:90)
    at org.jboss.weld.environment.se.Weld.initialize(Weld.java:790)
    at org.abondar.experimental.javaeedemo.basiccdi.Main.main(Main.java:10)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
    at java.base/java.lang.Thread.run(Thread.java:844)
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-9,5,org.abondar.experimental.javaeedemo.basiccdi.Main] was interrupted but is still alive after waiting at least 15000msecs
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-9,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-2,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-11,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-4,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-13,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-6,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] thread Thread[ForkJoinPool.commonPool-worker-15,5,org.abondar.experimental.javaeedemo.basiccdi.Main] will linger despite being asked to die via interruption
[WARNING] NOTE: 7 thread(s) did not finish despite being asked to  via interruption. This is not a problem with exec:java, it is a problem with the running code. Although not serious, it should be remedied.
[WARNING] Couldn't destroy threadgroup org.codehaus.mojo.exec.ExecJavaMojo$IsolatedThreadGroup[name=org.abondar.experimental.javaeedemo.basiccdi.Main,maxpri=10]
java.lang.IllegalThreadStateException
    at java.base/java.lang.ThreadGroup.destroy(ThreadGroup.java:776)
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:321)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

Upvotes: 1

Views: 2336

Answers (1)

John Ament
John Ament

Reputation: 11733

Ok, thanks for the clarifying content.

The problem is that since you're using Weld SE, your beans.xml may not be used the way you're expecting. Specifically, bean discovery works differently in SE mode. The error you get is pretty clear - you're missing @Dependent on the interceptor class.

Another way to do what you're trying to do is to use SeContainerInitializer from CDI:

SeContainer container = SeContainerInitializer.newInstance()
        .enableInterceptors(LoggingInterceptor.class)
        .selectAlternatives(MockGenerator.class).initialize();

But I believe you actually found a bug in Weld, since you're using all, all beans should have been discovered.

Upvotes: 2

Related Questions