Spring 3 Tutorials

The Spring Framework is an open source application framework and Inversion of Control container for the Java platform.

Logo_Spring_258x151This page lists down all tutorials on this website available on this topic. This page will be updated everytime, I will write any post on Spring 3. Feel free to suggest topics you want to read more on.

Hello world application

Spring 3 mvc hello world application with maven and jstl

Learn to create basic Spring mvc application using maven for dependency management.

Best Practices

13 best practices for writing spring configuration files

Spring beans, dependencies, and the services needed by beans are specified in xml configuration files or annotations. The XML configuration files, however, are verbose and more clean. If not planned and written correctly, it becomes very hard to manage in big projects.
In this article, I will show you 13 best practices for writing spring XML configurations.

Spring core

Spring bean scopes

The beans in spring container can be created in five scopes. singleton, prototype, request, session and global-session. Learn them in detail.

Spring bean life cycle call back methods

Spring framework provides following 4 ways for controlling life cycle events of bean:

  1. InitializingBean and DisposableBean callback interfaces
  2. Other Aware interfaces for specific behavior
  3. custom init() and destroy() methods in bean configuration file
  4. @PostConstruct and @PreDestroy annotations

Read more in above post.

Spring beans autowiring

In spring framework, setting bean dependencies in configuration files is a good practice to follow, but the spring container is also able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory. Learn how to do it.

Bean autowire byType

Autowiring by type allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. Learn in detail.

Bean autowire byName

Autowiring by name allows a property to be autowired such that it will inspect the container and look for a bean named exactly the same as the property which needs to be autowired. Learn more.

Bean autowire by constructor

Autowiring by constructor is similar to byType, but applies to constructor arguments. In autowire enabled bean, it will look for class type of constructor arguments, and then do a autowire by type on all constructor arguments. Learn more.

Bean autowire by autodetect

Autowiring by autodetect uses either of two modes i.e. constructor or byType modes. First it will try to look for valid constructor with arguments, If found the constructor mode is chosen. If there is no constructor defined in bean, or explicit default no-args constructor is present, the autowire byType mode is chosen. Learn more.

Inversion of control and dependency injection

Inversion of control (IoC) and dependency injection (DI) patterns

In traditional programming, the flow of the business logic is determined by objects that are statically assigned to one another. With inversion of control, the flow depends on the object graph that is instantiated by the assembler and is made possible by object interactions being defined through abstractions. The binding process is achieved through dependency injection. Read this post for a detailed information.

Spring MVC

Configure beans in spring 3.x using @Configuration annotation

Spring 3 gave the freedom to move bean definitions out of xml files. Now, you can give bean definitions in your java files itself. This is called JavaConfig feature (using @Configuration annotation). In this post, we will see a example of this type of configuration.

How to handle exceptions in spring 3 mvc using @ExceptionHandler annotation

Every good open source framework allows to write the exception handlers in such a way that you can separate then from your application code. Well, Spring 3 also allows you to do so using annotation @ExceptionHandler.  Lets see how all this works.

How to use interceptors with spring 3 mvc

Interceptors, as we know, are special web programming constructs which gets invoked every time when a certain pre-configured web request is made. They are always the most important and basic functional segments designed very early in product life cycle, due to their importance. In this post, I am writing about using these interceptors in your spring 3 application. This post is very short to focus only on interceptor configuration and usage.

How to add jstl support in spring 3 using maven

In this post, I am writing about adding JSTL support to Spring 3 using maven build tool.

Spring 3 internationalization (i18n) support

Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes.Learn to make Spring 3 application supported in multiple languages and locales.

Spring Security

Login form example using default user service in configuration file

Spring security is another major module in spring distribution and is supported only for applications developed using JDK 1.5 or higher. In this post, I will demostrate how to secure you spring 3 based web application. The scope of this tutorial is to:

  • Only authorized user should be able to access edit employee screen.
  • Unauthorized users should be presented with login screen.
  • Successful credentials should forward to edit employee screen.
  • Unsuccessful credentials should forward to access denied screen.
  • There should be a link for logout of the application.

JDBC based user service example

Commonly there are two ways of storing user details for authentication i.e. using custom user service implementation or spring provided jdbc user service. In this post, I am showing the way to use second approach i.e. jdbc user service.

HTTP basic authentication example

Basic authentication is often used with stateless clients which pass their credentials on each request. It’s quite common to use it in combination with form-based authentication where an application is used through both a browser-based user interface and as a web-service. Lets learn how to configure this in a simple spring 3 based web application.

Custom user details service example

Commonly there are two ways of storing user details for authentication i.e. using custom user service implementation or spring provided jdbc user service. In this post, I am showing the way to use first approach i.e. using custom user service implementation. In summary, UserDetailsService interface is used in order to lookup the username, password and GrantedAuthorities for any given user. Lets see everything in action.

Method level security example using @Secured and @PreAuthorize

@PreAuthorize and @Secured annotations are used for method level security in Spring 3 application. It’s different from other mechanism in sense that others are used for URL based security. Lets see how it works.

Spring 3 security siteminder pre-authentication example

In this example, user has been pre-authenticated in any other application and get into your web application using site minder. Site minder sends a request header about pre-authenticated user which you can utilize to further authorize the user inside your application. You do not need to further authenticate the user, just verify user roles from database and provide appropriate access inside application.

View layer security using jsp taglibs

Most security implementations usually are on controller or model layer of MVC. Spring provides security in view layer also. It is mostly needed when we want to hide certain links or buttons based on user’s role so that he will not be able to access that functionality. It is done using taglibs. Lets learn how to achieve it.

Web services support

How to write RESTful web services using spring 3 mvc

A step by step guide for creating  a RESTful web service application using Spring 3 mvc. This post covers all requited dependencies, annotations applied and a working application demo, which you can download for free.

ORM integrations

Spring 3 and hibernate integration tutorial with example

This tutorial is focused on usage of Hibernate with Spring 3 framework. I will show that how a basic end to end application flow looks like as a result of this integration.

Spring 3.2.5.RELEASE and Hibernate 4 Integration Example

In previous example, lots of people struggled in resolving maven dependencies. In this example, I have added all required jar files in project sourcecode itself. Additionally, I have advanced the version of spring from 3.0.5 to 3.2.5.RELEASE.

3rd party integration

Spring 3 standalone integration with hornetq

HornetQ is an open source project to build a multi-protocol, embeddable, very high performance, clustered, asynchronous messaging system. HornetQ comes with a lot of flexibility to be configured with some existing application frameworks. In this post, I am demonstrating the usage of HornetQ with Spring 3.

Useful Examples

Spring Email : JavaMailSender example

A simple example to show how to send emails with Spring 3

Spring AbstractRoutingDataSource example

AbstractRoutingDataSource is very useful feature in spring framework if you do have a design which permits multiple databases based on certain criteria which may change for each user request. An example can be use of database. You can use a particular database when user belongs to certain locale and switch to another locale if user belongs to another locale.

Unit testing and related

Unit testing authorization in junit

The main motive of this post to describe the way to build fully populated authentication object programmatically and then use it in unit testing and possibly in application code itself.


Spring 3 documentation

23 thoughts on “Spring 3 Tutorials

  1. Rajesh R says:

    Out standing and very easy understanding tutorial. Lokesh! please keep it up the good work.I never read such a good tutorial.If possible please post EJB tutorial and interview questions.

  2. maheshpkp says:

    I want to get a job in Java Web development.presently i am learning spring framework. for the java web development what are the main concept i want to learn.please guide me.

  3. Pallavi K says:

    Hi, All your posts are really nice and helpful.
    Can u please share an example for a Spring Task Executor for uploading a file Asynchronously. I need to develope the task executor which will upload the file asynchronously. The Task executor will have a maximum and minimum pool size of 1 and a queue size of 20 to ensure that only one file can be uploaded at any one time but that subsequent file uploads can be queued.
    Once the files are uploaded or queued for uploading(in a separate thread) then a response will be returned to the user to indicate that the uploading have been started.

    1. pankaj kumar says:

      really as we know spring aop is heart of spring too define the cross cutting concern , i also expect that lokesh will describe it very well.

      1. pankaj kumar says:

        i want to add one more thing,please provide the cglib and proxy pattern also, because i have encountered no of
        times how it works internally with spring container

    1. Nitin, It depends on person to person, and how much he wants to learn. Regarding interview, on a good day you will be asked nothing. On a bad day, nothing will be asked you know.

      So, my suggestion will be learn something “*everyday*” until you succeed in your next interview.

Comments are closed.