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.
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?
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.
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.
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.
Vaibhav G Garg
posted 8 years ago
Thanks Ken and Ankit for clarifications.
catch it before it slithers away! Oh wait, it's a tiny ad: