Reputation: 9737
I am a little curious about project setup.
I have a project which I am trying to update.
So I peppered some of my concrete classes with the @Named("") annotation. The class actually extends an abstract class which implements an interface. I want to inject the named class on a class that is not included in that jar. What are the considerations that I need to keep in mind when doing that?
Upvotes: 0
Views: 283
Reputation: 6753
Your question is very vague, so don't expect this answer to stun you with details.
The class actually extends an abstract class which implements an interface.
This shouldn't be a problem - the final bean instance will have the the type of all the classes and interfaces in the hierarchy. Nothing to worry about.
I want to inject the named class on a class that is not included in that jar.
Firstly, you can only inject beans into another beans. That means your other class (which is not in the same jar) has to become a bean. For this you enable the CDI in the other archive and annotate the class with desired scope.
Secondly, the other jar, has to have a dependency on the one with @Named
beans so that the CDI container can see those beans.
Last but not least, make sure you use adequate scopes on beans. The scope belongs to certain context and each has a different lifecycle. So for instance @ApplicationScoped
beans live from app start-up till shutdown, while @RequestScoped
are only active during a HTTP request.
Upvotes: 1