struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peterus Greatus <>
Subject Re: How do my Actions get to Hibernate so they can persist my objects?
Date Wed, 11 Mar 2009 16:44:30 GMT

Wes Wannemacher wrote:
> Spring is not as hard as it appears. The Spring libraries are dauntingly
> big, 
> but in reality, the core of spring is actually quite easy to understand,
> you 
> just have to wrap your mind around Inversion of Control. It will come 
> naturally if you've used it before, but if Spring is your first venture,
> bare 
> with it a bit, you'll be glad you did.

Hi Wes,
 I took your advice and read Spring in Action to get a handle on what is
going on. Its pretty clear to me now how it can be the missing-link between
Struts2 an Hibernate. I have to say I'm glad I took the time to get to grips
with Spring - it seems pretty handy.

Wes Wannemacher wrote:
> The piece you're missing here is that Spring services can easily be wired
> into 
> your actions. If you have a bean named springBean defined in your 
> applicationContext.xml, then have the spring plugin installed in your
> struts 
> app. Then, you have a setter on your action called 
> setSpringBean(SpringBeanInterface sbi), the plugin will see the setter and 
> give you a copy of the service. It's pretty slick and saves you quite a
> bit of 
> time and exception handling.

The other part of this patch work is that the developers of Hibernate,
Spring and Struts2 have accommodated each other by providing integration
Struts2 says I'll let you use an object factory if you want. In steps
Hibernate says you can use my Session Factory to make session objects.
Spring covers this.

What troubled me was in Spring I couldn't see how it used Hibernate - in the
config it does not reference Hibernate classes - only its own Hibernate
module - which in turn (I now know) relies on Hibernate being in the

In the Struts2 examples I was looking for programmatic dependencies to make
sense of the relationships ("where are the imports?"). DI via Spring makes
it a configured and configurable (wiring) dependency.

Thanks for helping me make sense of this. I'm new to DI and I started out
wanting to use Struts2 - all the examples in books heavily rely on
additional tools (Ant, Maven) and frameworks. Its getting the complete
system configured that has taken so much time versus using Struts2. They all
aim to get the whole system going on one step. From a learning point of view
I don't care if I throw away code that is tightly coupled because I didn't
follow a flexible design pattern. The code served a purpose - to help me
learn so when I develop the next part/version I see the value in the more
powerful tools (rather than use them because all the cool kids are). What I
wanted from the books I read was for an outside-looking-in explanation of
all the parts to the system. I had to get inside to understand it all. I
think this is missing from all the blogs and tutorials out there. 


> I read this guys post here
> So I’m
> pretty certain I need to be using Spring.
> To me it seems like Spring is a pre-requisite for Java development. Maybe
> I
> need to be drinking the Spring Kool-Aid.

The need to use Spring really increases proportional to the size of the 
project. It is definitely another layer of complexity, but it is worth it if 
you are building an application to scale up over time. At the same time, the 
more you use it, the more you'll find it helping you. The wiring of 
dependencies is just one piece, there are also quite a few other helpful 
advantages, AOP, transaction management, and mock objects to name a few.

Again, this will seem like a setup for my book, but I am currently writing
with the intention to introduce people into modern development techniques
this. I take the approach that Struts is a common piece, but that most 
applications will have the need to utilize other libraries such as Spring, 
Guice, EJB or any number of other helper frameworks. 



Wes Wannemacher
Author - Struts 2 In Practice 
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more

To unsubscribe, e-mail:
For additional commands, e-mail:

View this message in context:
Sent from the Struts - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message