• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

struts2 & spring integration - what's so good about IoC / dependency injection

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I just integrated spring with struts and I just wonder 'what's the big deal about IoC'? (I'm asking this in the most respectful way of course).

So, i take it I can do this from the action:



So, I defined the PersonManager in the applicationContext:



and the 'personManager' was initialized on startup.

My point is this: I could have done this on startup and add 'personManager ' to the 'session'.
When I ask for a service (say registrationService to persist a new user) I use a singleton and then I can write
a method (getRegistrationService) in the base class and it's accessible for any 'Action'

* can anyone give a better example (real-life) when to use IoC (or dependency injection).. a good solution for some problem?

Thank you (and hope to learn something new)


 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, you're not really using dependency injection there. DI means that the bean is injected *external* to the class. In your example the action is still managing the instantiation, the only difference being that the implementation is decided in an external configuration file. This is the weakest form of "DI" possible, if it would even be called that.

Consider this instead:The PersonManager implementation is still defined in a Spring config, as before. Here's the difference: now you can instantiate TheAction and set its PersonManager implementation in arbitrary code--like a unit test, or on action instantiation (like if you're using the S2 Spring plugin).

With the implementation decided by external means we can now play a lot of games with both the action and the PersonManager implementation. We could, for example, have a PersonManager implementation that always throws a specific exception. Or disallows login, or whatever--thus giving us the ability to create unit tests for any aspect of system functionality.

There are a million web references for IoC/DI--it's usually a situation where until you use it and understand why it useful you won't "get it", like Lisp macros. It's easier to understand by utilization. The first step is to understand what it actually *is*; hopefully the simplistic example above has at least cleared up your initial misconceptions.
 
Peter Primrose
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep!

I tried it with:

private PersonManager personManager;
.
.
.
public void setPersonManager(PersonManager personManager)
.
.
.

//action
System.out.println("ok...."+personManager.getPeopleSize());

and it worked....OK...good idea! I'll look for more ideas how to utilize it ...surely I'll use my 'services' there.


Thank you David!
 
reply
    Bookmark Topic Watch Topic
  • New Topic