Spring

   Today we all know that Spring is being widely used in enterprise applications because of its easy of development and layered architecture.  Which enable us to integrate its various layers with other technologies and API(s), like - JMS, MVC, Junit, EJB, Hibernate and so on.
  This framework is very popular because of  its great feature of Dependency Injection (DI).

Let us look a simple example to understand DI:-

I have a class A (singleton class) that has some parameters that sets at start up time. Also. I have a class B (prototype) that needs the parameters in class A. Now, I am trying to figure out how to get the instance of A which is to be use in B later sometime.
  1. class A { //singleton class
  2.       String a;
  3. }
  1. class B {      //prototype class
  2.      A a;
  3.     String b;
  4.     public B createB() {  //for method lookup
  5.          return null;
  6.     }
  7. }

In config file:-

< bean id="A" class="A" scope="singleton" > < /bean >
< bean id="B" class="B" scope="prototype" >
< property name="b" value="xyz" / >
< lookup-method name="createB" bean="A" / >
< /bean >

In some Java file, let us use the objects:

B b = ctx.getBean("B"); //Here, the object of B is fetched from container which was already created at start up of container.Also,the object of A is available inside B.

Now, the question is How does the above config file creates and injects the object.

To know the answer, let us look more detail in config.xml file mentioned above. This file should havae these following entries as well so that it communicates with Spring Framwwork to meet the job.
config.xml:

< context-param >
< param-name >contextConfigLocation< /param-name >
< param-value >/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml< /param-value >
</ context-param >

< !-- Either use this Listener :-- >
< listener >
< listener-class >org.springframework.web.context.ContextLoaderListener< /listener-class >
< /listener >

< !-- or use the ContextLoaderServlet instead of the above listener < servlet >
< servlet-name >context< /servlet-name >
< servlet-class >org.springframework.web.context.ContextLoaderServlet< /servlet-class >
< load-on-startup >1< /load-on-startup >
</ servlet >
-->

Explanation:-
The listener inspects the 'contextConfigLocation' parameter. If the parameter does not exist, the listener will use /WEB-INF/applicationContext.xml as a default. When it does exist, it will separate the String using predefined delimiters (comma, semi-colon and whitespace) and use the values as locations where application contexts will be searched for. Ant-style path patterns are supported as well: e.g. /WEB-INF/*Context.xml (for all files whose name ends with "Context.xml", residing in the "WEB-INF" directory) or /WEB-INF/**/*Context.xml (for all such files in any subdirectory of "WEB-INF").

The ContextLoaderServlet can be used instead of the ContextLoaderListener. The servlet will use the 'contextConfigLocation' parameter just as the listener does.

Also, B b = ctx.getBean("B"); How Does this work ?
Let us see how spring understands thatctx in context and it is already aware of the config file.

Say, we have a class called 'Operation.java', like:

public class Operation implements ApplicationContextAware{

Public ApplicationContext ctx;
public void setApplicationContext(ApplicationContext ctx) { // This method is mandatory as it is declared in ApplicationContextAware' interface. This line of code lets spring Framework to set(and hence get) the 'ctx' .

this.ctx = ctx;
}


B b = ctx.getBean("B");

}

Comments

Popular posts from this blog

Java

RTC - Repository : How to Revert Back the Changes