Social Icons

Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Spring 3 - Complete Bean & Annotation based configuration, No XMLs used

With the advent of Spring 3 there has been an widespread use of annotations and annotations based configurations. It brings down the development time and also has it's own advantages & disadvantages.

However there are very few examples online which shows a complete Spring MVC application configured using ONLY bean based configuration. In most of the cases you will find atleast the dispatcher-servlet.xml lying around even in a Bean based configuration.

So, let's see how can we get rid of any Spring related XML files for configuration and develop a Spring MVC application. First let's take a look at the application's entry point and the only XML file of any standard web application web.xml:




Note that we have configured ContextLoaderListener to use AnnotationConfigWebApplicationContext instead of the default XmlWebApplicationContext and added contextConfigLocation as the Beans Class which will contain the spring configuration.

Now, let's see the the Bean based configuration class:



You don't have to declare your bean in your context file in this bean based config, instead Annotate the class with @Component, @Service, @Controller or @Repository and our @ComponentScan annotation in BeansConfig.java will automatically register them in Spring context. Your Bean configuration class should be annotated with @Configuration annotation.

Make sure you mark Service classes with @Service annotation and not with @Component as the former is the more specialized annotation for Service components. Similarly, @Controller for Controller components and @Repository for DAO components in your application.


The controller below shows also the use of @Autowired annotation for auto-wiring and injecting OfficeAddress into Employee bean.

Download


Download the Eclipse project SpringDIAndAutowired.zip




Spring 2.0 MVC - MultiActionController

Let's check out a simple example to use Spring 2.0 MultiActionController

Notably, Spring provides a handful number of controllers each of which has their advantages of implementation. Personally, I prefer and use MultiActionController more often, mainly due to the way it actually works!


What you need ?

Eclipse - Download from http://www.eclipse.org
Spring 2.0 Libraries - Download from http://www.springsource.org/download/
JSTL 1.2.1 Libraries - Download from http://jstl.java.net/
Commons Logging Libraries - Download from http://commons.apache.org/logging/download_logging.cgi
Log4j Libraries - Download from http://logging.apache.org/log4j/1.2/download.html

Jars to be present in WEB-INF/lib folder
commons-logging-1.1.1.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
log4j-1.2.14.jar
spring.jar

Now that we have all required libraries ready, let's look at what changes / additions you need to do to a empty web app to make it Spring enabled.

web.xml


Configure the dispatcher servlet in web.xml and servlet-mapping to intercept the requests.

DemoApp-servlet.xml

The SimpleUrlHandlerMapping maps the URLs to each Controller beans.
The ParameterMethodNameResolver defines that method name to be executed must be reolved from the URL parameter, while the defaultMethodName is set to 'loadPage'

AuthenticationController.java

This is the Controller class. Note that loadPage method is automatically executed when request comes to /AuthenticationController.do
If the request contains any param like /AuthenticationController.do?action=doLogout then doLogout will be executed. ModelAndView object is returned, which is mapped by the 'viewresolver' configuration in DemoApp-servlet.xml

index.jsp

Note the form action here.


home.jsp

Note the href tag for logout here.


 Download Eclipse project  Spring2MultiActionController.zip