You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Musachy Barroso <mu...@gmail.com> on 2008/04/21 20:37:31 UTC

xwork and DI

If I want to add a constant that can be injected in one of xwork
classes (like "devMode" for example), what do I need to do? Just
adding @Inject and defining the constant in default.properties in core
doesn't work. I don't know much about this whole guice black magic
thing :)

musachy

-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: xwork and DI

Posted by Wes Wannemacher <we...@wantii.com>.
Not the object you are injecting, but the object that is being
injected... 

Like here - 

http://struts.apache.org/2.x/struts2-core/apidocs/com/opensymphony/xwork2/inject/Container.html

Foo has dependencies injected, but if you instantiate it as 

Foo foo = new Foo();

Then you won't get anything injected. But, if you want a fully injected
copy of Foo, you need to get it from the container - 

Foo foo = c.inject(Foo.class);

So, if you are '@Inject'ing your String, you have to make sure that the
object that you are trying to read your String from is created by the
Container.

An easy example is com.opensymphony.xwork2.ObjectFactory, the method - 

public Object buildBean(String className, Map extraContext, boolean
injectInternal) throws Exception 

which calls - 

protected Object injectInternalBeans(Object obj) {
        if (obj != null && container != null) {
            container.inject(obj);
        }
        return obj;
    }

IIRC, all beans pass through here at some point.

-Wes



On Mon, 2008-04-21 at 21:52 -0400, Musachy Barroso wrote:
> It is just an string, like "devMode".
> 
> musachy
> 
> On Mon, Apr 21, 2008 at 8:00 PM, Wes Wannemacher <we...@wantii.com> wrote:
> > Musachy,
> >
> >  Is your object (injectee) instantiated by the container? I think there
> >  is some work to do after instantiating so that the dependencies can be
> >  injected.
> >
> >  -Wes
> >
> >
> >  On Mon, 2008-04-21 at 14:37 -0400, Musachy Barroso wrote:
> >  > If I want to add a constant that can be injected in one of xwork
> >  > classes (like "devMode" for example), what do I need to do? Just
> >  > adding @Inject and defining the constant in default.properties in core
> >  > doesn't work. I don't know much about this whole guice black magic
> >  > thing :)
> >  >
> >  > musachy
> >  >
> >
> >
> >  ---------------------------------------------------------------------
> >  To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
> >  For additional commands, e-mail: dev-help@struts.apache.org
> >
> >
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: xwork and DI

Posted by Musachy Barroso <mu...@gmail.com>.
It is just an string, like "devMode".

musachy

On Mon, Apr 21, 2008 at 8:00 PM, Wes Wannemacher <we...@wantii.com> wrote:
> Musachy,
>
>  Is your object (injectee) instantiated by the container? I think there
>  is some work to do after instantiating so that the dependencies can be
>  injected.
>
>  -Wes
>
>
>  On Mon, 2008-04-21 at 14:37 -0400, Musachy Barroso wrote:
>  > If I want to add a constant that can be injected in one of xwork
>  > classes (like "devMode" for example), what do I need to do? Just
>  > adding @Inject and defining the constant in default.properties in core
>  > doesn't work. I don't know much about this whole guice black magic
>  > thing :)
>  >
>  > musachy
>  >
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>  For additional commands, e-mail: dev-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: xwork and DI

Posted by Wes Wannemacher <we...@wantii.com>.
Musachy, 

Is your object (injectee) instantiated by the container? I think there
is some work to do after instantiating so that the dependencies can be
injected. 

-Wes

On Mon, 2008-04-21 at 14:37 -0400, Musachy Barroso wrote:
> If I want to add a constant that can be injected in one of xwork
> classes (like "devMode" for example), what do I need to do? Just
> adding @Inject and defining the constant in default.properties in core
> doesn't work. I don't know much about this whole guice black magic
> thing :)
> 
> musachy
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: xwork and DI

Posted by Brian Pontarelli <br...@pontarelli.com>.
You can definitely inject from any of the XWork files, 
struts-default.properties, struts.properties, struts.xml, 
struts-default.xml, struts-plugin.xml. These are all configured in the 
Dispatcher when the Container is created (I think).

-bp

Musachy Barroso wrote:
> I tried that, I think the problem is where to define the constant. I
> used default.properties, where the others are defined, but the
> container complains that it cannot satisfy the dependency. Of all
> examples I found on xwork, only one references the bean to be injected
> by name (which might not be working either).
>
> musachy
>
> On Mon, Apr 21, 2008 at 10:57 PM, Jeromy Evans
> <je...@blueskyminds.com.au> wrote:
>   
>> Musachy Barroso wrote:
>>
>>     
>>> If I want to add a constant that can be injected in one of xwork
>>> classes (like "devMode" for example), what do I need to do? Just
>>> adding @Inject and defining the constant in default.properties in core
>>> doesn't work. I don't know much about this whole guice black magic
>>> thing :)
>>>
>>> musachy
>>>
>>>
>>>
>>>       
>>  Here's some simple examples:
>>
>>  Setup some beans and constants in the Container with unique names:
>>
>>
>> http://code.google.com/p/struts2urlplugin/source/browse/trunk/struts2-url-plugin/src/main/resources/struts-plugin.xml
>>
>>  eg. 1. Inject constant into a bean
>>
>>
>> http://code.google.com/p/struts2urlplugin/source/browse/trunk/struts2-url-plugin/src/main/java/com/blueskyminds/struts2/urlplugin/URLPatternActionMapper.java
>>  Line 333
>>   @Inject("struts.urlplugin.actionMapConfiguration")
>>    public void setMappingConfigurationName(String mappingConfigurationName)
>> {
>>        this.mappingConfigurationName = mappingConfigurationName;
>>    }
>>
>>  eg. 2. Inject Container into a bean to enable run-time lookup by name and
>> class:
>>   @Inject
>>    public void setContainer(Container container) {
>>        this.container = container;
>>    }
>>   Line 116
>>   mappingConfiguration = container.getInstance(ActionMapConfiguration.class,
>> mappingConfigurationName);
>>
>>  eg. 3. Inject Bean into another bean directly: exactly as per the container
>> injection.  If there's only one instance it will bind, otherwise specify the
>> name, otherwise it will fail.
>>  @Inject("defaultActionMapper")
>>  public void setActionMapper(ActionMapper actionMapper) {...
>>
>>  It only gets complicated when you need to mix injection with beans created
>> outside the container, as described by Wes.
>>
>>  cheers,
>>  Jeromy Evans
>>
>>  PS. this is far inferior to the current functionality of Guice.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>  ---------------------------------------------------------------------
>>  To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>>  For additional commands, e-mail: dev-help@struts.apache.org
>>
>>
>>     
>
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: xwork and DI

Posted by Musachy Barroso <mu...@gmail.com>.
I tried that, I think the problem is where to define the constant. I
used default.properties, where the others are defined, but the
container complains that it cannot satisfy the dependency. Of all
examples I found on xwork, only one references the bean to be injected
by name (which might not be working either).

musachy

On Mon, Apr 21, 2008 at 10:57 PM, Jeromy Evans
<je...@blueskyminds.com.au> wrote:
> Musachy Barroso wrote:
>
> > If I want to add a constant that can be injected in one of xwork
> > classes (like "devMode" for example), what do I need to do? Just
> > adding @Inject and defining the constant in default.properties in core
> > doesn't work. I don't know much about this whole guice black magic
> > thing :)
> >
> > musachy
> >
> >
> >
>  Here's some simple examples:
>
>  Setup some beans and constants in the Container with unique names:
>
>
> http://code.google.com/p/struts2urlplugin/source/browse/trunk/struts2-url-plugin/src/main/resources/struts-plugin.xml
>
>  eg. 1. Inject constant into a bean
>
>
> http://code.google.com/p/struts2urlplugin/source/browse/trunk/struts2-url-plugin/src/main/java/com/blueskyminds/struts2/urlplugin/URLPatternActionMapper.java
>  Line 333
>   @Inject("struts.urlplugin.actionMapConfiguration")
>    public void setMappingConfigurationName(String mappingConfigurationName)
> {
>        this.mappingConfigurationName = mappingConfigurationName;
>    }
>
>  eg. 2. Inject Container into a bean to enable run-time lookup by name and
> class:
>   @Inject
>    public void setContainer(Container container) {
>        this.container = container;
>    }
>   Line 116
>   mappingConfiguration = container.getInstance(ActionMapConfiguration.class,
> mappingConfigurationName);
>
>  eg. 3. Inject Bean into another bean directly: exactly as per the container
> injection.  If there's only one instance it will bind, otherwise specify the
> name, otherwise it will fail.
>  @Inject("defaultActionMapper")
>  public void setActionMapper(ActionMapper actionMapper) {...
>
>  It only gets complicated when you need to mix injection with beans created
> outside the container, as described by Wes.
>
>  cheers,
>  Jeromy Evans
>
>  PS. this is far inferior to the current functionality of Guice.
>
>
>
>
>
>
>
>
>
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>  For additional commands, e-mail: dev-help@struts.apache.org
>
>



-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: xwork and DI

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Musachy Barroso wrote:
> If I want to add a constant that can be injected in one of xwork
> classes (like "devMode" for example), what do I need to do? Just
> adding @Inject and defining the constant in default.properties in core
> doesn't work. I don't know much about this whole guice black magic
> thing :)
>
> musachy
>
>   
Here's some simple examples:

Setup some beans and constants in the Container with unique names:

http://code.google.com/p/struts2urlplugin/source/browse/trunk/struts2-url-plugin/src/main/resources/struts-plugin.xml

eg. 1. Inject constant into a bean

http://code.google.com/p/struts2urlplugin/source/browse/trunk/struts2-url-plugin/src/main/java/com/blueskyminds/struts2/urlplugin/URLPatternActionMapper.java
Line 333
   @Inject("struts.urlplugin.actionMapConfiguration")
    public void setMappingConfigurationName(String 
mappingConfigurationName) {
        this.mappingConfigurationName = mappingConfigurationName;
    }

eg. 2. Inject Container into a bean to enable run-time lookup by name 
and class:
   @Inject
    public void setContainer(Container container) {
        this.container = container;
    }
  Line 116
   mappingConfiguration = 
container.getInstance(ActionMapConfiguration.class, 
mappingConfigurationName);

eg. 3. Inject Bean into another bean directly: exactly as per the 
container injection.  If there's only one instance it will bind, 
otherwise specify the name, otherwise it will fail.
@Inject("defaultActionMapper")
public void setActionMapper(ActionMapper actionMapper) {...

It only gets complicated when you need to mix injection with beans 
created outside the container, as described by Wes.

cheers,
 Jeromy Evans

PS. this is far inferior to the current functionality of Guice.









---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org