Right way to use @Produce method

cdi, java, javafx-2, jboss-weld

Solution

You seem to mix CDI Extensions with producers. First, if you want to have a producer, the factory method should return a `NewsListView`, not a generic type. Using `@Producer` along with a qualifier annotation will bind with the annotated type. So there is no need to annotate `NewsListView` with `@FXMLManaged`. Then you inject your `NewsListView` somewhere in a bean.

Producing the view:

public class FXMLManagedProducer {
    @Produces @FXMLManaged
    public NewsListView getFXMLManagedInstance() {
        return new NewsListView();
    }
}

Using the producer:

public class SomeBean {
    @Inject @FXMLManaged
    NewsListView view;
}

But my guess is that this is not what you are looking for. I think you might want to create a CDI Extension

public class YourExtension implements Extension {

    <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) {
        if(pat.getAnnotatedType().isAnnotationPresent(FXMLManaged.class)) {
            // do your stuff here
        }
    } 
}

This way you would process your annotated `NewsListView`. You may want to have a look at other methods to hook into the lifecycle, so you can create the bean and inject dependencies if neccessary.

Problem

I'm trying to combine CDI (weld-se 2) and JavaFX and I want to annotate my controller class with custom created annotation so this class creation is managed using my factory method. I suppose that is should looks like below but this code is not working. Could you possibly advice what should be changed? Annotation: ``` @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE }) @Qualifier public @interface FXMLManaged { } ``` Factory class: ``` public class FXMLManagedProducer { @Produces @FXMLManaged public <T> T getFXMLManagedInstance(Class<T> type) { return type.newInstance(); } } ``` Controller class: ``` @FXMLManaged public class NewsListView { } ```

Original source