struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Aum Strut <aum.str...@gmail.com>
Subject Re: How to get instance of the given dependency using Container
Date Mon, 01 Oct 2012 03:19:58 GMT
Thanks Gabriel for the help, but i am a bit confused about it.

Can you please help me to understand why we are using those two entries
like
one is a constant and other is a bean class?

while my understanding was that all i need to define a constant and based
on that i can get the actual class (extending MyCustomClass) from the
container.

I am still not sure about the use of bean? and if i go with your opinion
say

public class MyClass{
   private Class<? extends MyCustomClass> provider;

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

Won't above statement will give an error "Type mismatch: cannot convert
from MyCustomClass to Class<? extends MyCustomClass>" ?

Thanks
-U
On Mon, Oct 1, 2012 at 6:43 AM, Gabriel Belingueres
<belingueres@gmail.com>wrote:

> 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.struts@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-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message