0

A Java EE web application is using a library that implement a class with injected objects.

First, I declare a default producer in the library that provides part of dependent objects. Is it possible to overwrite this producer by another one declared in the web application using @Alternative annotation ?

Second, I declare a class that implement an interface using @Default annotation. Declaring a class using @Alternative anotation in the web application and updating correspondng with this class does not replace the default bean. It's working fine with other interfaces. Why ?

Source code exemple for the second case

Loader class:

@Dependent
@Default
public class DefaultLoader implements MyLoader {

    @Inject
    protected DefaultLoader(MyBinder binder) {
        map = binder.getMap();
    }

    ...

}

Default binder class:

@Dependent
@Default
public class DefaultBinder implements MyBinder {

    public DefaultBinder() {
        define();
    }

    ...

}

Alternative binder class:

@Dependent
@Alternative
public class ProtoBinder extends DefaultBinder {

    public ProtoBinder() {
        super();
    }

    ...

}

beans.xml

<?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" version="1.1"
    bean-discovery-mode="annotated">
    <alternatives>
        <class>com.proto.ProtoBinder</class>
    </alternatives>
</beans>
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
ruddy32
  • 21
  • 7
  • It seems to be a problem with CDI 1.x, tit is not possible to overwrite a library bean outside the library - cf. http://stackoverflow.com/questions/18575174/java-ee6-override-cdi-alternative. – ruddy32 Aug 28 '15 at 05:46

1 Answers1

0

@Alternative is only relevant with other @Alternative implementation. It will not override a @Default qualified bean. If you really want to go down this path then you might want to have a look at @Specializes.

Franck
  • 1,754
  • 1
  • 13
  • 14