3 Replies Latest reply on Oct 7, 2013 3:22 PM by dudehook

    Getting the instance of a bean from InjetionPoint?


      I am trying to achieve a way to dynamically inject an object into a field, but based on some dynamic run-time data such as a query.


      For instance:


      public class ServiceUsingData {

         @Inject @Queried("findThingById") SomeThing thing;


         public void someServiceMethod() {




         public String findThingById() {

            // This will return the query criteria to be used by the provider to find the thing.

            return "select * from things where id = ?";




      I know I could create a provider method which could receive the InjectionPoint and get the string from the @Queried qualifier... but then how do I get the instance of the bean being injected?


      Also, I haven't figured out how to populate the parameter of the query yet.  What I'm trying to go for is a declarative way for a developer to define a query and have the container use it to inject the results.  Maybe this is the wrong approach for that?


      Any pointers?

        • 1. Re: Getting the instance of a bean from InjetionPoint?

          If 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);

          1 of 1 people found this helpful
          • 2. Re: Getting the instance of a bean from InjetionPoint?

            Hi 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:


            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:


            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

            1 of 1 people found this helpful
            • 3. Re: Getting the instance of a bean from InjetionPoint?

              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.