Define stereotype to use on different project

65 Views Asked by At

I have 2 java-ee (helidon-mp) projects (Lets say A and B), both share some logic which is encapsulated on a separate project/jar/dependency, the projects are similar yet not identical, to handle those differences I consider it would be a good idea to create a Stereotype which would allow me to specify when do I want to create/provide instances tuned for either A or B project. Given the shared dependency project creates beans it has its own beans.xml file, just as both of the projects that depend on it.

The problem

I would like to be able to specify the desired stereotype to use within the beans.xml file of each project, yet when I specify the stereotype at the project level, such configuration is not passed down to the dependency project; this throws an exception with a message similar to the following one:

WELD-001408: Unsatisfied dependencies for type NeededClassForAProject with qualifiers @Default
  at injection point [BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public the.project.class.injecting.dependency.ProjectA(NeededClassForAProject)
  at the.project.class.injecting.dependency.ProjectA.<init>(ProjectA.java:0)

When I define the stereotype within the dependency beans.xml everything works like a charm yet this causes that whenever I need to work with Project A I need to update (and rebuild) the dependency project changing its beans.xml indicating I want to use the configuration for A and same goes whenever I switch to working with project B

The question itself:

Is it possible to keep my configuration at the project level beans.xml and make the dependency project recognize such configuration from the parent project? how

1

There are 1 best solutions below

0
Ordiel On

The answer is here

where it clearly states:

By default, @Alternative beans are disabled. We need to enable an alternative in the beans.xml descriptor of a bean archive to make it available for instantiation and injection. However, this activation only applies to the beans in that archive.

From CDI 1.1 onwards the alternative can be enabled for the whole application using @Priority annotation.