File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Doubts about Dependency Injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Doubts about Dependency Injection" Watch "Doubts about Dependency Injection" New topic
Author

Doubts about Dependency Injection

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
I have confusions about the Dependency Injection concepts.

1. I have seen in the Spring MVC framework that we don't get the beans from the bean factory/application context explicitly ourselves. Then, how the beans are getting initialized/instantiated and by whom? Refer the XML and code snippet below.

2. Referring to the code snippet given below:

XML File:


Java Code Snippet:


Here, in above code, I am not able to understand how it is providing us Loose Coupling. The Controller class depends upon the TestService property. So, whenever we need to update the TestService, we need to change the java controller class code as well as XML code. So, it seems to have tight coupling rather than loosely coupled as some of the books suggest in such cases. Please let me know what am I missing over here?
Ken Rimple
author
Ranch Hand

Joined: Jul 20, 2010
Posts: 63

You get loose coupling by using interfaces. If you had a TestService interface, and define your business methods in that file, you can then create any number of implementations, so that you are not linking to the implementation and can replace it. Maybe your data service can return class instances of your results, and you can start with a jdbc implelentation, but then move to Hibernate and create another implementation.

You can create stub implementations for testing, or use any mocking service such as easymock, mockito or jmock.

In addition, you can delegate operations to other beans, so you can inject a business service into a controller, hence your controller doesn't contain business logic.

Ken


Ken Rimple
ken.rimple@gmail.com
Co-author, Manning Spring Roo in Action
I teach for Chariot Solutions (Spring, Maven, Hibernate, other courses). Schedule here
Ken Rimple
author
Ranch Hand

Joined: Jul 20, 2010
Posts: 63

In response to #1, you are running Spring so the DispacherServlet mounts controllers, and creates them.

In Spring, you can configure the container for annotation injection, and then Spring injects any services you autowire with the @Autowired annotation. You also reference the instance of the implementing class. Your example uses xml, so your service implementation would be the class referenced in the XML, but the Interface would be referenced in the class that needs your service.
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
Thanks Ken for your response. I will brush up my concepts based on your response and will post if would have any further queries.
Ankit Khandelwal
Greenhorn

Joined: Feb 21, 2012
Posts: 13
Vaibhav

Here i am trying to tell you about the loose coupling provided by Spring but before that i want to brief you about interface.

When you say i am using interface which means you are going to have a flexibility in your code that for same set of method signatures you can have multiple implementation e.g.

You create an interface A which has two methods , find and findAll , now you may have two implementation class B and C of this interface , and when you say implementation of this interface means you have a class which will contain the definition of these two methods. It is like we all have mouth as an interface but we could pronounciate "Hello" in different ways.

So let say today wherever you have to instantiate of A in your code , you do



and you did this instantiation in 100 place in your whole code.

so tomorrow if you want to change the implemenation by class C , will you change the all classes... if yes it would be very tedious.

So by using spring , you just need to change the implementation of that interface in your XML file at once and you will start getting new implementation in your whole code...

That is why we say Spring provides loose coupling.

I am also learning Spring and if i am wrong anywhere in my explanation any one also suggest me.


Thanks
Ankit
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
Thanks Ken and Ankit for clarifications.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubts about Dependency Injection