-
1. Re: Getting the instance of a bean from InjetionPoint?
luksa Oct 7, 2013 2:01 PM (in response to dudehook)1 of 1 people found this helpfulIf you don't want to use a producer, you can add the following field to SomeThing:
@Inject InjectionPoint injectionPoint;
Keep in mind that SomeThing must be dependent scoped in this case.
if you want to use a producer method, you can get an instance of SomeThing by adding a parameter to the producer method (along with the InjectionPoint parameter) like this:
@Produces SomeThing produceSomeThing(SomeThing someThing, InjectionPoint ip);
-
2. Re: Getting the instance of a bean from InjetionPoint?
rsmeral Oct 7, 2013 2:02 PM (in response to dudehook)1 of 1 people found this helpfulHi David,
I think it could be done using a producer which reads the @Queried annotation, you wouldn't even need to know the instance for this.
But for a cleaner approach, try looking at DeltaSpike (http://deltaspike.apache.org), it's 'data' module serves a purpose very similar to what you are asking. It allows you to define repositories of entities with declarative query methods, like this:
@Repository public abstract class SomeThingRepository extends AbstractEntityRepository<SomeThing, Long> { @Query("select s from SomeThing s where s.id = ?1") public abstract SomeThing findById(Long id); }
You then inject this repository and use its methods. But for basic queries, like searching by primary key, it provides a basic repository implementation with common methods, so it can be even simpler:
@Repository public interface SomeThingRepo extends EntityRepository<SomeThing, Long>{}
and then inject and use methods of EntityRepository, like findBy(PK).
Alternatively, you could use DeltaSpike's PartialBean module to implement a custom way for the annotation-driven querying, a simple example here: https://github.com/jboss-developer/jboss-wfk-quickstarts/tree/master/deltaspike-partialbean-advanced.
Lastly, there's one more DeltaSpike quickstart which demonstrates just what you've asked - actual injection parameterized by a non-binding annotation on the injection point. The way this works is, that at deployment time, the extension creates a bean and its contextual lifecycle for every injection point: https://github.com/jboss-developer/jboss-wfk-quickstarts/tree/master/deltaspike-beanbuilder
DeltaSpike is, sadly, poorly documented at this time, so stop by #deltaspike on Freenode or use users@deltaspike.apache.org in case of questions.
Hopefully, someone can chip in with some non-DeltaSpike solutions
-
3. Re: Getting the instance of a bean from InjetionPoint?
dudehook Oct 7, 2013 3:22 PM (in response to luksa)Thanks, Marko - That does answer my exact question. I'll be able to use that in my platform, I'm sure.
And thanks, Ron - your answer is exactly what I needed for declarative querying. I do have a couple of follow up questions on the partial bean and bean builder stuff, but that is probably better suited for the deltaspike forums... so I'll pop over there with those at some point.
Thanks!