Struts 2 Spring Plugin Usage

View: New views
7 Messages — Rating Filter:   Alert me  

Struts 2 Spring Plugin Usage

by RogerV :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In Mannings "Struts 2 in Action" there's an example of Spring setter-injection by simply specifying the bean to be injected with a basic applicationContext.xml like;

<beans>
<bean id="securityManager" class="com.myApp.security.PasswordChecker" />
</beans>

and everywhere where my action classes have a setSecurityManager() method the PasswordChecker is injected. This works brilliantly.

However how far down the "tree" can I go with this simple approach? For example, the securityManager in turn has a setService(Service service) method, and defining an additional <bean id="service" class="........"/> is not working as expected - the injection is not happening whereas

<bean id="securityManager"
        class="com.myApp.security.PasswordChecker">
        <property name="service"><ref local="Service"/>
        </property>
</bean>

<bean id="Service" class="com.myApp.services.ServicesImpl"/>

injects the SecurityManager with the service and then injects the SecurityManager into my Action. Have I missed something or is this working as it should and I'm misunderstanding the book?

Regards

RE: Struts 2 Spring Plugin Usage

by James Cook-13 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey Roger,

If I am understanding you then that is fine.

I often do the same, as I like to have a DAO as well as a Service layer.
So inject my DAO's into my Service and my Service into my actions.

Good ole' code by interface approach :)

-----Original Message-----
From: RogerV [mailto:roger.varley@...]
Sent: 02 November 2009 16:16
To: user@...
Subject: Struts 2 Spring Plugin Usage


In Mannings "Struts 2 in Action" there's an example of Spring
setter-injection by simply specifying the bean to be injected with a
basic
applicationContext.xml like;

<beans>
<bean id="securityManager" class="com.myApp.security.PasswordChecker" />
</beans>

and everywhere where my action classes have a setSecurityManager()
method
the PasswordChecker is injected. This works brilliantly.

However how far down the "tree" can I go with this simple approach? For
example, the securityManager in turn has a setService(Service service)
method, and defining an additional <bean id="service" class="........"/>
is
not working as expected - the injection is not happening whereas

<bean id="securityManager"
        class="com.myApp.security.PasswordChecker">
        <property name="service"><ref local="Service"/>
        </property>
</bean>

<bean id="Service" class="com.myApp.services.ServicesImpl"/>

injects the SecurityManager with the service and then injects the
SecurityManager into my Action. Have I missed something or is this
working
as it should and I'm misunderstanding the book?

Regards
--
View this message in context:
http://old.nabble.com/Struts-2-Spring-Plugin-Usage-tp26157722p26157722.h
tml
Sent from the Struts - User mailing list archive at Nabble.com.


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


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


Re: Struts 2 Spring Plugin Usage

by RogerV :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>
> If I am understanding you then that is fine.
>
> I often do the same, as I like to have a DAO as well as a Service layer.
> So inject my DAO's into my Service and my Service into my actions.
>
> Good ole' code by interface approach :)
>

It's the means that I have to employ to go down the next level I was
questioning. Whereas the action gets injected with a simple <bean
id="securityManager" class="com.myApp.security.PasswordChecker" />, I
have to use the longer version <bean id="securityManager"
       class="com.myApp.security.PasswordChecker">
       <property name="service"><ref local="Service"/>
       </property>
</bean>
to get things injected into my bean. I was wondering why just
declaring the service bean in the same way as the SecurityManager
doesn't seem to work.

Mind you, it's nice to know my design approach isn't totally AWOL as well.

Regards

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


RE: Struts 2 Spring Plugin Usage

by James Cook-13 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That is because of the default autowiring.

I use the convention plugin for Struts and I take advantage of the
spring auto wiring (by name) to automagically inject my service layer.
You can do the same for the rest. Check out the reference guide over at
spring framework to do so. Weirdly I like having my DAO and Service
classes in the appcontext xml file.. But then that is because I am
weird...

-----Original Message-----
From: Roger Varley [mailto:roger.varley@...]
Sent: 02 November 2009 17:22
To: Struts Users Mailing List
Subject: Re: Struts 2 Spring Plugin Usage

>
> If I am understanding you then that is fine.
>
> I often do the same, as I like to have a DAO as well as a Service
layer.
> So inject my DAO's into my Service and my Service into my actions.
>
> Good ole' code by interface approach :)
>

It's the means that I have to employ to go down the next level I was
questioning. Whereas the action gets injected with a simple <bean
id="securityManager" class="com.myApp.security.PasswordChecker" />, I
have to use the longer version <bean id="securityManager"
       class="com.myApp.security.PasswordChecker">
       <property name="service"><ref local="Service"/>
       </property>
</bean>
to get things injected into my bean. I was wondering why just
declaring the service bean in the same way as the SecurityManager
doesn't seem to work.

Mind you, it's nice to know my design approach isn't totally AWOL as
well.

Regards

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


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


Re: Struts 2 Spring Plugin Usage

by RogerV :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>
> I use the convention plugin for Struts and I take advantage of the
> spring auto wiring (by name) to automagically inject my service layer.
> You can do the same for the rest. Check out the reference guide over at
> spring framework to do so. Weirdly I like having my DAO and Service
> classes in the appcontext xml file.. But then that is because I am
> weird...
>

Either I'm misunderstanding the responses or I'm not being clear (I'm
also using the convention plugin along with the Spring plugin.)

<bean id="securityManager" class="com.myApp.security.PasswordChecker"
/> is sufficient to have the PasswordChecker injected everywhere any
action contains a getSecurityManager() method.

However if my PasswordChecker also has need of injection to access a
database (ie. PaswordChecker contains a getDaoManager() method then
simply having

<bean id="securityManager" class="com.myApp.security.PasswordChecker" />
<bean id="daoManager" class="com.myApp.security.DaoManager"/>

in the application.context.xml does not appear to be sufficient. My
action is injected with the PasswordChecker, but the PasswordChecker
is not injected with the DaoManager.

Regards

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


RE: Struts 2 Spring Plugin Usage

by James Cook-13 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The spring plugin will look by default auto wire your spring beans via
type, it isn't tied to the getXXX(); method call. I myself only have the
declaration and a setter, the autowiring injects the spring managed bean
for me.

Nothing is done with the getters....

DAO Class:

public MyDAOImpl implements MyDAO...

Service Class:

public MyServiceImpl implements MyService {

private MyDAO myDAO;

public void setMyDAO(MyDAO myDAO) {
        this.myDAO = myDAO;
}
.....

Action:

public MyAction extends ActionSupport {

private MyService myService;

public void setMyService(MyService myService) {
        this.myService = myService;
}
.....

appContext.xml

<bean id="myService" class="com.mypackage.service.impl.MyServiceImpl" >
        <property name="myDAO" ref="myDAO" />
</bean>

<bean id="myDAO" class="com.mypackage.dao.impl.MyDAOImpl" />
.....

I hope my example helps in some way...


-----Original Message-----
From: Roger Varley [mailto:roger.varley@...]
Sent: 03 November 2009 12:49
To: Struts Users Mailing List
Subject: Re: Struts 2 Spring Plugin Usage

>
> I use the convention plugin for Struts and I take advantage of the
> spring auto wiring (by name) to automagically inject my service layer.
> You can do the same for the rest. Check out the reference guide over
at
> spring framework to do so. Weirdly I like having my DAO and Service
> classes in the appcontext xml file.. But then that is because I am
> weird...
>

Either I'm misunderstanding the responses or I'm not being clear (I'm
also using the convention plugin along with the Spring plugin.)

<bean id="securityManager" class="com.myApp.security.PasswordChecker"
/> is sufficient to have the PasswordChecker injected everywhere any
action contains a getSecurityManager() method.

However if my PasswordChecker also has need of injection to access a
database (ie. PaswordChecker contains a getDaoManager() method then
simply having

<bean id="securityManager" class="com.myApp.security.PasswordChecker" />
<bean id="daoManager" class="com.myApp.security.DaoManager"/>

in the application.context.xml does not appear to be sufficient. My
action is injected with the PasswordChecker, but the PasswordChecker
is not injected with the DaoManager.

Regards

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


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


RE: Struts 2 Spring Plugin Usage

by mgainty :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


roger-

i would factor the Database injection for your DAO to use getSessionFactory to acquire factory which contains Hibernate dataSource (which contains startup Database parameters)
http://www.java2s.com/Code/Java/Hibernate/SpringDaoInjection.htm

other solutions?
Martin Gainty
______________________________________________
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
 
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.




> Date: Tue, 3 Nov 2009 14:49:07 +0200
> Subject: Re: Struts 2 Spring Plugin Usage
> From: roger.varley@...
> To: user@...
>
> >
> > I use the convention plugin for Struts and I take advantage of the
> > spring auto wiring (by name) to automagically inject my service layer.
> > You can do the same for the rest. Check out the reference guide over at
> > spring framework to do so. Weirdly I like having my DAO and Service
> > classes in the appcontext xml file.. But then that is because I am
> > weird...
> >
>
> Either I'm misunderstanding the responses or I'm not being clear (I'm
> also using the convention plugin along with the Spring plugin.)
>
> <bean id="securityManager" class="com.myApp.security.PasswordChecker"
> /> is sufficient to have the PasswordChecker injected everywhere any
> action contains a getSecurityManager() method.
>
> However if my PasswordChecker also has need of injection to access a
> database (ie. PaswordChecker contains a getDaoManager() method then
> simply having
>
> <bean id="securityManager" class="com.myApp.security.PasswordChecker" />
> <bean id="daoManager" class="com.myApp.security.DaoManager"/>
>
> in the application.context.xml does not appear to be sufficient. My
> action is injected with the PasswordChecker, but the PasswordChecker
> is not injected with the DaoManager.
>
> Regards
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@...
> For additional commands, e-mail: user-help@...
>
     
_________________________________________________________________
Hotmail: Trusted email with Microsoft's powerful SPAM protection.
http://clk.atdmt.com/GBL/go/177141664/direct/01/
http://clk.atdmt.com/GBL/go/177141664/direct/01/