You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sonny Gill <so...@gmail.com> on 2009/02/12 06:19:43 UTC

Using Struts 2 native dependency injection

Hi guys,

I am trying to find out if it is possible to do some basic DI using
the native DI support in Struts 2.

I can create a bean in struts.xml as -

<bean name="beanName" type="com.MyInterface" class="com.MyImpl" />

and have it injected into my Action using -

@Inject( "beanName" )

Is there a way to pass a primitive or String value to the bean
beanName created using the <bean /> element?

Say something like -
<bean name="beanName" type="com.MyInterface" class="com.MyImpl" >
    <property><name>myString</name><value>Abc</value>
</bean>


Cheers,
Sonny

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


Re: Using Struts 2 native dependency injection

Posted by Sonny Gill <so...@gmail.com>.
> There are warnings from time to time that we don't expose the details
> of the internal DI because we want to reserve the right to change the
> inner workings. If you are familiar with Spring, go ahead and use it,
> despite all of the available features, it will only bring in what you
> want to use... If you like the @Inject annotation, there is also a
> GUICE plugin that works pretty well. Guice is easy to learn and quick
> to implement.

Of course. I guess I was just scratching an itch :)

I wouldn't use the Struts internal stuff on a substantial project.

But, it is good to know that this option exists. I was just putting
together a simple Configuration UI for an application, where most of
the actions needed one service to be injected, which is done in a
single parent class. If the internal DI is ever changed, it will be
quick enough to fix it in that one place.

Thanks for the warning. I should make sure that I don't suggest doing
this to anyone else.

Cheers,
Sonny

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


Re: Using Struts 2 native dependency injection

Posted by Wes Wannemacher <we...@wantii.com>.
On Thu, Feb 12, 2009 at 7:47 AM, Sonny Gill <so...@gmail.com> wrote:
> That's partly the reason I was trying to do this.
> With Guice already included in Struts, it somehow feels wrong to add
> Spring just for very basic DI.
>
> Sonny


There are warnings from time to time that we don't expose the details
of the internal DI because we want to reserve the right to change the
inner workings. If you are familiar with Spring, go ahead and use it,
despite all of the available features, it will only bring in what you
want to use... If you like the @Inject annotation, there is also a
GUICE plugin that works pretty well. Guice is easy to learn and quick
to implement.



-- 
Wes Wannemacher
Author - Struts 2 In Practice
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher

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


Re: Using Struts 2 native dependency injection

Posted by Sonny Gill <so...@gmail.com>.
That's partly the reason I was trying to do this.
With Guice already included in Struts, it somehow feels wrong to add
Spring just for very basic DI.

Sonny

On Thu, Feb 12, 2009 at 8:18 PM, Wes Wannemacher <we...@wantii.com> wrote:
> It's part of Struts, we're using a custom/hacked/forked version of Guice
> within XWork.

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


Re: Using Struts 2 native dependency injection

Posted by Wes Wannemacher <we...@wantii.com>.
On Thursday 12 February 2009 06:08:59 Ddlc wrote:
> Is that @Inject annotation part of Struts 2?
>
> Or are you using something like, for example, Guice?

It's part of Struts, we're using a custom/hacked/forked version of Guice 
within XWork. 

-Wes

-- 

Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more
http://www.manning.com/wannemacher


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


Re: Using Struts 2 native dependency injection

Posted by Ddlc <re...@hotmail.com>.
Is that @Inject annotation part of Struts 2?

Or are you using something like, for example, Guice?
-- 
View this message in context: http://www.nabble.com/Using-Struts-2-native-dependency-injection-tp21970139p21973978.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Using Struts 2 native dependency injection

Posted by Sonny Gill <so...@gmail.com>.
Well, I have made a little more progress..

We can define a 'constant' element like -
  <constant name="myVal" value="x" />

Then that can be injected in to the bean property using @Inject annotation.

The only issue is that the setter for the property has to accept a
String value, and then convert into the required type.
This is actually good enough for what I was trying to do, which was to
use the Struts' built in DI facilities for very simple cases.

Still, if anyone knows how I can avoid doing the type conversion
myself, that would be great.

Cheers,
Sonny



On Thu, Feb 12, 2009 at 1:19 PM, Sonny Gill <so...@gmail.com> wrote:
> Hi guys,
>
> I am trying to find out if it is possible to do some basic DI using
> the native DI support in Struts 2.
>
> I can create a bean in struts.xml as -
>
> <bean name="beanName" type="com.MyInterface" class="com.MyImpl" />
>
> and have it injected into my Action using -
>
> @Inject( "beanName" )
>
> Is there a way to pass a primitive or String value to the bean
> beanName created using the <bean /> element?
>
> Say something like -
> <bean name="beanName" type="com.MyInterface" class="com.MyImpl" >
>    <property><name>myString</name><value>Abc</value>
> </bean>
>
>
> Cheers,
> Sonny

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