Search code examples
javacdiglassfish-4java-ee-7weld

CDI producer method not working if defining class doesn't declare a scope


I'm running some proof of concept in a local Glassfish 4.1.1 (WELD 2.2.2) installation about CDI.

I've created a class bean named TipicalBean which injects an integer produced in a producer method.

The class defining the producer method looks like this

public class ProducerTest {

    @Produces @MyNumber @Dependent public static int getMyNumber() {
        return 100;
    }
}

This doesn't work. Even if I declare the method static

But it does work if if put the @Dependent scope in the class definition, like this:

@Dependent
public class ProducerTest {

In addition, if I declare that producer method in the same class which is injecting it, I can define the @Dependent scope in the producer method declaration, but WELD prints a warning in Glassfish console

WELD-000018: Executing producer field or method [BackedAnnotatedMethod] @Produces
@MyNumber @Dependent public mypackage.TipicalBean.produceMyNumber() on incomplete
declaring bean Managed Bean [class mypackage.TipicalBean] with qualifiers [@Any
@Default] due to circular injection

Why's that? Is that Producer Methods must be defined within ManagedBeans?

As I'm using JavaEE 7, I have not created the beans.xml file.

Thanks


Solution

  • From the CDI spec:

    A producer method must be a default-access, public, protected or private, non-abstract method of a managed bean class or session bean class.