Friday, April 26, 2013

How to start with Spring ..? Simple Way ..


Spring is one of the most popular java Frameworks available in the market. A framework makes developer easy to code and helps in increasing productivity. It follows MVC design pattern where Dispatcher Servlet acts as Controller. Spring can be used for both web and stanalone applications depends on the requirement.


In order to work with spring first of all you need to download spring distribution from the official site. here is the  link

Once you have spring distribution, download the eclipse from the link

Let's start:

Create a project in Eclipse and add the required jar's (spring jar's) which we have downloaded.

Now create applicationContext.xml insode WEB-INF folder. once created, add the xml schemas to the xml as shown here.

applicationContext.xml file will be used by the server to create the global context(spring container). We will configure the beans in our project with the help of <bean> tag inside root tag <beans>.

Now, open the deployment descriptor, web.xml and add the listener,context param and the Dispatcher Servlet as shown.

Here we have configures a listener called ContextLoaderListener with the help of <listener>. With the help of <context-param> we have provided location of our configuration file (applicationContext.xml) .

Create on more xml with the name spring-servlet.xml inside WEB-INF directory.

What's happening when  we deploy our project:

When we deploy our project into server , initially server will creates ServletContext object which will rise an event. As we know, whenever an event rised corresponding handler will be executed. Here ContextLoaderListener is the corresponding handler(listener) which contains mainly two methods. They are contextInitialised() and contextDestroyed(). When ever server creates ServletContext object immediately contextInitialised() method will be called by the server. As part of contextInitialised() Spring vendors has provided code to find the applicationContext.xml and creates the spring container(global context).

A spring container is an object which will take care of creating bean objects and calling corresponding methods and removing their objects, which is called bean life cycle.

Whenever we undeploy the project from the server, contextDestroyed() method will be called and the corresponding object will be removed.

And now, server finds the <load-on-startup> and creates object to the DispatcherServlet which is configured in web.xml.Immediately server will call the init() of DispatcherServlet where it will finds the corresponding xml with that name (spring here).

As the name of the servlet is spring , server will get the name from the web.xml with the help of config.getServletName() and appends "-servlet.xml" to that name. So finally it will checks for the file "spring-servlet.xml" and creates context which is Web application context .

Now, create a package in src folder of the project.Create a class SampleController which implements Controller interface. We need to provide implementation to the abstract methods in Controller interface.

handleRequest() is the abstract method available in Controller interface. Here is the implementation of the abstract method.

Here, success is the name of the jsp which will be placed inside jsp folder which is created inside WEB-INF folder.Configure a bean with name viewResolver in spring-servlet.xml.Here is the folder structure.

InternalResourceViewResolver is the predefined class which have properties prefix and suffix as shown. SampleController is the controller class we have created and configured in spring-servlet.xml.

Now in the web.xml we have mentioned welcome file as redirect.jsp. Here is the redirect.jsp.

In the redirect.jsp we are forwarding request to the controller whose name is configured as /sample. 

Now configure Tomcat server in your eclipse IDE. To configure Server, open server view which is available in Window --> Show View --> Servers.

In the servers view, right click and chose New --> Server and provide the server details as shown.

Now, right click on the project and select Run as --> Run on Server. Project will be deployed into the server.

Once project successfully deployed , here is the output.

What's happening when we run the project:

When we run our application, server will get the details from web.xml. As we have mentioned welcome file server will render the response.jsp where we have use <jsp:forward> with path given as /sample. 

Now, the spring container which was already created when we deploy the project, will get's the uri '/sample' and checks in web.xml for appropriate url mapping. As we have use ' / ' for DispatcherServlet url mapping container will call service() of DispatcherServlet.

As part of service() container will finds any bean whose name matches with '/name'. As it founds SampleController it will starts executing handleRequest() of SampleController.

As part of SampleController we are returning ModelAndView object with view name given as success. Once the method got executed, it will return ModelAndView object to DispatcherServlet which further passes the information about view(success) to InternalResourceViewResolver. Now InternalResourceViewResolver will get's the jsp name and it will append prefix(/WEB-INF) and suffix (.jsp) to the view (success).

So finally we are getting the output from success.jsp in the browser.

Download Source

1 comment: