This is a two step process: You can define a String property to hold
the "name" of the required instance to inject, and then inject the
appropiate instance with the given name.

For example you can define a constant:

  <constant name="my.constant" value="non-default-instance" />

  <bean type="com.foo.MyCustomClass" name="non-default-instance"
    class="com.foo.MyCustomClassProvider" />



@Inject(value="my.constant")
public void setConstant(String value) {
  this.constant = constant;
}

public initialization() {
  provider = container.getInstance(MyCustomClass.class, constant);
}

HTH,
Gabriel


2012/9/30 Aum Strut <aum.str...@gmail.com>:
> Hi All,
>
> I am working on a requirement where i need to get instance of given class
> at run time using Struts2 Container and here is my Code
>
> public class MyClass{
>
>   private Class<? extends MyCustomClass> provider;
>
>  public MyClass(@inject Container container){
>
>     provider=container.getInstance();  // this is where i got strucked
>
>  }
>
> }
>
> Since Provider can by anyone who extends MyCustomClass so i am not
> sure before hand which implementation will be provided at run time.I
> am not sure how to get instance in this case from container.
>
> for e.g At run time there can be Either ClassA or ClassB or may be
> ClassC which all extends MyCustomClass.
>
> How can i pass this information to the container to get the instance
> of the class passed at run time?
>
>
> Thanks in advance
>
> -a

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to