Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 2nd, 2006, 03:56 AM
Registered User
 
Join Date: Jan 2006
Location: hyderabad, Andhra Pradhesh, India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Spring Framework

Tell me clearly about Inversion of control in spring framework with a single and simple example. I could'nt able get to a conclusion on reading the articles present in net.

M Kalyan Tilak
Reply With Quote
  #2 (permalink)  
Old February 2nd, 2006, 06:28 PM
Authorized User
 
Join Date: Jan 2006
Location: , , USA.
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to longjava
Default

Inversion Control, also known as Dependency Injection, is a concept that separates the dependency between objects which makes it easy for testing classes, and easy to replace different object without affecting the client code.

For example:
public class B
{
   public void foo()
   {
      // do something
   }
}

public class A
{
   B obj;

   public A()
   {
       // Here object A create an object B. Therefore object A depends on object B.
       obj = new B();
   }

   public void doSomething()
   {
      obj.foo();
   }
}

The example above shows objects A and B are tightly couple since object A is aware of how to create object B.

======================================

Here is an example version of how to use the Inversion Control:
public interface IFoo
{
   public void foo();
}

public class B implement IFoo
{
   public void foo()
   {
      // provide implementation here
   }
}

public class A
{
   private IFoo obj;

   public void setFoo( IFoo foo )
   {
      obj = foo;
   }

   public void doSomething()
   {
      obj.foo();
   }
}

If you look at how Class A is defined, it is no longer responsible for creating an object B, instead it provides an method setFoo() to allow any object that implements IFoo interface to be injected by invoking this method. Injection an object into another is usually done by a container via configuraion file. This is actually how Spring framework actually does.

Hope this help.


Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Spring Framework 2 cateyesmale All Other Wrox Books 2 December 7th, 2007 10:03 AM
changing host server framework 2.0 from framework jay_vijesh ASP.NET 1.x and 2.0 Application Design 1 September 11th, 2006 02:18 PM
How use framework components in compact framework? r_bazghaleh C# 0 February 19th, 2006 05:05 AM
Spring Framework(java) dtrprasad BOOK: Professional Java Development with the Spring Framework 0 October 3rd, 2005 08:05 PM
Java Spring Framework (Code Download) edwin_craig BOOK: Professional Java Development with the Spring Framework 0 September 29th, 2005 09:08 AM



All times are GMT -4. The time now is 06:17 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.