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>