Resolution of external (third-party) beans in welding

I know that it is still not very popular, since the specification was released just a few months ago.

I have not “set up” welding yet, I am just reading, and on this issue I want to make sure that I understand this important point correctly:

Is permission of beans in third-party banks achieved by declaring them as <alternatives> in your beans.xml ?

If not, how to use beans from third-party libraries that don't have beans.xml ?

Putting jar in the classpath will not work if there is no beans.xml in META-INF , which cannot be done for third-party banks. (see Gavin King related post )

+6
java cdi jboss-weld jsr299
source share
2 answers

why is it so difficult?

Just create a Maker method for these third-party classes.

Suppose you have a third-party library that automatically takes PDF files and sends them by facsimile, and you like to use something like

 private @Inject PdfFaxService faxService; 

in your code, then you can just provide this producer method. PdfFaxService works stateless, so we can safely assume that we can do it @ApplicationScoped :

 public @Produces @ApplicationScoped PdfFaxService createFaxService() { return new PdfFaxService(initparameters); } 

somewhere.

NTN.

+6
source share

My understanding of the alternative is that it is an alternative to some other interface implementation that you can use in a different deployment environment (for example, in a testing environment). An alternate bean is declared by annotating it using @Alternative .

To use the alternative in this deployment scenario, you select it in the <alternatives> element of the CDI META-INF/beans.xml deployment descriptor. This will enable @Alternative beans, which are disabled by default.

If enabled, if the container detects an ambiguous relationship for a given injection point, it will consider alternatives that can be introduced, and if there is exactly one, select this alternative.

In other words, alternatives are a good way to replace an existing implementation with another during deployment. If there is nothing to replace, you do not need alternatives, just put your jar on the class path. I’m not sure that this was exactly your question, although I doubt the concept of third-party cans.

More details in 2.1.4. Alternatives 4.6. Alternatives and 4.7. Eliminating unsatisfied and ambiguous dependencies (but I think this is what you read).

Update: To answer your additional question.

If not, how to use beans from third-party libraries that don't have beans.xml

This cannot be, the bean archive must have bean.xml (let it be empty), as described in section 15.6. Documentation packaging and deployment :

CDI does not define any special deployment archives. You can pack beans in a JAR, EJB-JAR, or WARs-any deployment location in a CLASSPATH application. However, the archive must be a "bean archive". This means that each archive containing beans must specify a file named beans.xml in the META-INF class path directory or WEB-INF web root directory (for WAR). The file may be empty. beans are deployed in archives that are not in the beans.xml file beans.xml not be available for use in the application.

Then, to correct the unsatisfied and controversial relationship, refer to the previously mentioned section 4.7.

Update 2: It seems that when using BeforeBeanDiscovery.addAnnotatedType() you can add other classes that must be considered when a bean is detected. ( BeforeBeanDiscovery - event)

+3
source share

All Articles