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 17:12:11 GMT
I am still confused the way Struts2 internal DI and container.getInstance()
working
since i have this inside my class

private Class provider;
public void setProvider( Class<? extends Provider> provider)
    {
        this.provider = provider;
    }

So if at run time i want to inject my provider, so i will be declaring a
constant for my plugin say

<constant name="provider.name' value="com.abc.MyCustomProvider">

so i believe now i have to do the following steps

1. Get an instance of the provider based on the constant name
2. Init my provider with the instance i got from Struts2 DI

so i guess i need to do this either in init method or my bean constructor

 init(){

    setProvider(container.getInstance(Provider.class,"provider.name"));
}

but above will not work ,since as per the getInstance() method
signature <T>T getInstance
(Class<T> type, String name)
i will getting instance of Provider class and which means either i have to
change the signature of  public void setProvider( Class<? extends Provider>
provider)

which means loosing the generics capability as my intentions are to inject
any class which extends Provider.

i am all confused here and not sure what exact is to do, nor i want to add
any further dependencies.

Any suggestion as how to achieve this?

Thanks



On Mon, Oct 1, 2012 at 6:09 PM, Lukasz Lenart <lukaszlenart@apache.org>wrote:

> 2012/10/1 Aum Strut <aum.struts@gmail.com>:
> > i was thinking about a way like Spring DI used to do something like
> >
> > <bean type="com.foo.MyInterface" class="com.foo.MyImpl1" name="impl1" >
> >    <property name="a" value="Run time class"/>
> > </bean>
> >
> > It would be more convient to have injection like this way.
>
> Just annotate your setter with @Inject(name="a") and create constant
> in struts.xml. If "a" is a String value.
>
>
> Regards
> --
> Ɓukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> 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