简体   繁体   中英

Dependency Injection - using <jsp:include>

I am new to Spring and currently reading Spring concept - Dependency Injection.

It injects one object in another object by different methods.

My question is - Can <jsp:include> tag ( for ex. <jsp:include page="data.jsp" /> ) be an example of Dependency Injection ?

As it also includes one JSP contents in other. Please help me to confirm.

This is a interesting question, but I believe it doesn't. DI is more about automatic dependency injection. You declare the dependencies and someone, normally a Inversion of Control (IoC) container, injects those deps in your class.

Be aware that jsp is converted to a servlet class and <jsp:include> is a method call to another servlet class.

I suggest the above reading:

http://www.martinfowler.com/articles/injection.html

http://misko.hevery.com/code-reviewers-guide/

In <jsp:include> you have to specify what is the page name you are going to include. so that is kind of hard coding your dependency. Purpose of dependency injection is to separate dependent. That is not achieved through <jsp:include> .

  • In Dependency Injection or lesser used term Inversion of Control is actually The the passing of a dependency (a service) to a dependent object (a client) (Source: Wikipedia ). But this responsibility of passing or association is not done by the two objects in question themselves but some third entity, let's say Dependency Injector.

  • Now the very objective of DI is that the object to be associated with each other, do not do the association themselves but leave it to the Dependency Injector via some configuration.

  • Any Dependency Injector OR DI framework (eg Spring) is responsible for taking away the responsibility of creating and associating the objects from object themselves so that Object worry only about the business logic and leave the creation/configuration to the framework.

  • In the example of <jsp:include> , One JSP is including the other JSP, the JSPs themselves are assuming the responsibility of associating themselves hence this is NOT a DI example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM