View RSS Feed

Spring Framework

SpringSource Spring Framework tutorials and examples. Focusing on Spring 3 and above.

  1. Using Tiles in Spring MVC

    by , 11-27-2011 at 11:39 PM
    In earlier tips, I talked about using Spring MVC and the use of different view technologies. In this tip, I will look at Tiles as a view technology for Spring MVC. Tiles was initially built to be used with the Struts Framework. But as it is a powerful view technology it has been reused by other web frameworks. To use Tiles with Spring MVC you need to register the Tiles’ view resolver as a bean with the application’s xml file, springexamples.xml.

    Java Code:
    <bean class= "org.springframework.web.servlet.view.tiles2.TilesViewResolver"/>
    Tags: spring mvc, tiles Add / Edit Tags
    MVC , Tiles , Spring 3
  2. Using Annotations for Transactions in Spring

    by , 11-27-2011 at 11:38 PM
    In the previous tip, I showed you how you could use XML to define a transaction. In this tip we will look at doing the same using annotations. It is blindly simple to use annotations for transactions. The tx namespace provides the <tx:annotation-driven> element. It appears like this :

    Java Code:
    <tx:annotation-driven transaction-manager="txManager" />
    With this you can now define transaction rules very easily. This tag tells Spring to examine all beans in ...
  3. Configuring Transactions in XML with Spring

    by , 11-27-2011 at 11:33 PM
    Continuing from my previous post, I will demonstrate how you can declare transactions in XML using Spring. Continuing from the previous tips on transaction attributes, let’s dive straight in. Spring has a tx configuration namespace to simply declaring transactions in Spring. It is recommended that you include the aop namespace as well since some of the declarative transaction configuration elements use AOP configuration elements. The following shows how <tx:advice> is used for the Car service. ...

    Updated 11-27-2011 at 11:36 PM by Spring Framework

    Spring 3 , Configuration , Transaction , XML
    Attached Thumbnails Attached Images  
  4. Transaction Propagation Strategies in Spring

    by , 11-27-2011 at 11:29 PM
    In the previous tip, we looked at the key properties that are required for handling any transaction. In Spring, when you want to have your application are using. Spring transaction concepts are taken from the JEE specification specifically the EJB 3.0 specification. Another key aspect of managing transactions is the ability to control the propagation of transactions in your application. Spring has the org.springframework.transaction.TransactionStatus interface that allows an application to check ...
  5. Key Attributes of Spring Transactions

    by , 11-27-2011 at 11:25 PM
    Transactions are defined as doing a business unit of work. It is a key functionality that enable you to control concurrent access of data by multiple business operations. This is done in order to safeguard integrity and ensure that data remains in a consistent state. If for any reason the unit of work could not be completed as an indivisible unit of work, all the changes are unwound and the system is returned to its previous state. The key properties for the design of any application is to ensure ...

    Updated 11-30-2011 at 02:06 PM by Spring Framework

  6. How to Test Your Spring MVC Controller

    by , 11-27-2011 at 11:21 PM
    In my previous post, I showed you how to create a HomeController for our home page in your application. You might have noted that there was little code that linked your HomeController with Spring that was included. It is simply a plain old java object (POJO). In this tip, we will look at unit testing your controller. Unit testing your controller will be very easy since a POJO doesn’t need you to mock any Spring or other specific objects. I will create a test object using standard naming conventions, ...
    MVC , Controller , Test
  7. Define a Controller in Spring MVC

    by , 11-27-2011 at 11:17 PM
    When you work with Spring MVC, after configuring the Dispatcher Servlet in order to build an application you need to do the following:
    Define controllers that invoke business logic and create a ModelAndView object.
    Visualization component like JSP, Velocity or FreeMarker.
    Annotation configuration or XML to wire the components together

    The first thing that you will most likely do is to define a home page controller. Spring provides a number of controllers to use ...
    Spring 3 , MVC , Controller
  8. Configuring a Hibernate SessionFactory in Spring

    by , 11-27-2011 at 11:11 PM
    In an earlier post, I showed you how to integrate Hibernate with Spring using the annotation oriented persistence. For those still interested in the standard way I outline how you can accomplish that below. The key to the standard way is to use a Hibernate org.hibernate.SessionFactory for all access to the database through the domain objects. The Java configuration for the SessionFactory is shown below.

    Java Code:
    import org.springframework.beans.factory.annotation.Value;
    Tags: hibernate Add / Edit Tags
    Hibernate , Configuration , Spring 3
  9. Serving Static Content in Spring

    by , 11-27-2011 at 11:06 PM
    If you are concern you that DispatcherServlet will be handling static content requests, Spring has features that can help you to deal with this. Spring’s mvc namespace includes a <mvc:resources> element that handles requests for static content for you. In the following example, I show you how you can create an xml file that the DispatcherServlet can use to create an application context.

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns=""

    Updated 11-30-2011 at 01:59 PM by Spring Framework

  10. Spring MVC Basics

    by , 11-27-2011 at 11:03 PM
    Spring MVC’s key component is the DispatcherServlet. This servlet that functions as Spring MVC’s front controller. Like any servlet, DispatcherServlet must be configured in the web application’s web.xml file. If you need to setup the Dispatcher Servlet it is relatively straightforward. Below is what needs to be added to the web.xml file.

    Java Code:
    Tags: spring mvc Add / Edit Tags
    MVC , Spring 3
  11. Configuring a Spring Data Source Continued

    by , 11-27-2011 at 11:02 PM
    There are a number of DataSource connection pool implementations provided by different vendors as well as projects like C3PO and Apache Commons DBCP providing popular open-source options. It is easy to switch between different data source implementations, because of the common DataSource interface. Spring provides a convenient data source implementations as well but it is not considered as powerful as the vendor or open source implementations. The simplest one is org.springframework.jdbc.datasource.DriverManagerD ataSource, ...
  12. Writing A Namespace Handler in Spring

    by , 11-27-2011 at 10:58 PM
    Custom XML namespaces were introduced in Spring 2.0. Similar to Java packages, custom namespaces make it possible to define components without concern of colliding with other named components. Spring has already some predefined namespaces such as jee, aop, util, as well as others. Custom namespaces are handled using the NamespaceHandlerSupport class that generates metadata to be consumed by the Spring framework. This metadata is processed by parser classes that are also supplied by a component developer. ...
  13. Introducing Behaviors to Your Spring Beans

    by , 11-27-2011 at 10:55 PM
    In Spring AOP, you can use what is called an introduction (a special type of advice) that enables your objects to implement an interface dynamically, by providing the implementation class for that interface. This behavior can be multiplied to give the impression of multiple inheritance to your beans. To define an introduction, use the @DeclareParents annotation, which gives matching types a new parent.

    Java Code:
    package com.acme.springexamples.derivautos;
    import org.aspectj.lang.annotation.Aspect;
    Bean , Spring 3
  14. Configuring Data Sources in Spring

    by , 11-27-2011 at 10:51 PM
    Spring offers a number of options for configuring a data sources via data source beans. These sources include the following:
    • Data sources that use JNDI
    • Data sources that use JDBC drivers
    • Data sources that pool connections

    There is significant differences in the features available depending on what you are trying to do. In this tip, I will outline how to configure different data sources and what they are best used for.

    If you are looking to do really robust ...
  15. Reduce your Spring XML using Java Configuration

    by , 11-27-2011 at 10:47 PM
    As Spring has grown so has the need to configure more and more components leading to the xml configuration files growing larger and larger. In order to address this issue, the Java configuration project was started as a standalone project at SpringSource and developed over the years. It was finally integrated into the core Spring framework in version 3.0. Now you are able to use a powerful ApplicationContext configuration abstraction that has the same level of isolation as its XML cousin. In this ...
  16. Internationalization with Spring

    by , 11-27-2011 at 10:43 PM
    Often it is necessary for an application to be able to support several languages in order to address the requirements of customers located in different countries. For this java has support for what is called internationalization (i18n) that allows allows for an application to resolve text messages for different locales. The means of doing this in Java is to use what is called a ResourceBundle. These are property files that contain a key/message pair and have a special naming convention for the file ...
  17. How to Automatically Discover Spring Beans on the Classpath

    by , 11-27-2011 at 10:38 PM
    Since it’s appearance in Java 5, annotations has helped to simplify the configuration of application through component scanning. This removes the need for XML bean tags in many cases. Classes annotated as component stereotypes including the following: @Service, @Controller, @Configuration, @Component, @Repository as well as the JSR-330 annotations, such as @Named are included in the auto-scan default filter. Once auto-detected, these components become Spring beans and are automatically placed ...
  18. Using Spring Data Access Support with Hibernate

    by , 11-27-2011 at 10:35 PM
    In our previous tip, I gave an introduction to Spring’s persistence technology agnostic approach to data access. In this tip, we will show you how to use Spring with one of the most popular persistence technologies, Hibernate. Hibernate is an open source persistence framework that is one of the most successful persistence frameworks in the developer community. The reason for this is that it is a full featured object relational mapping (ORM) tool. It provides all the features you’d expect from a ...
  19. Using Spring with Hibernate

    by , 11-16-2011 at 06:43 PM
    In our previous tip, I gave an introduction to Spring’s persistence technology agnostic approach to data access. In this tip, we will show you how to use Spring with one of the most popular persistence technologies, Hibernate. Hibernate is an open source persistence framework that is one of the most successful persistence frameworks in the developer community. The reason for this is that it is a full featured object relational mapping (ORM) tool. It provides all the features you’d expect from a ...
  20. Database Support in Spring

    by , 11-16-2011 at 06:40 PM
    In our next tip, I will focus on Spring’s data access support. Spring uses the design pattern, data access object (DAO). The purpose of DAOs is to provide means to read and write data to the database. So what Spring has focused on is to make it easy for developers to write DAOs as irrespective of whether you use JDBC, Hibernate, JPA or JDO. The way in which it does this is by separating the interface from the data access implementation. Therefore the implementation can change to align with the ...
    Hibernate , JDBC , JPA
  21. Writing AOP in Spring

    by , 11-16-2011 at 06:35 PM
    In this last installment of Spring AOP, we will provide an example of how to use AOP in Spring.
    In the listing below, we add a car to an order.

    Java Code:
    package com.acme.springexamples.main;
    import com.acme.springexamples.service.CarService;
    import com.acme.springexamples.service.OrderService;
    public class Main { public static void main(String[] args) {
    	ApplicationContext context = 
    		new ClassPathXmlApplicationContext("META-INF/spring/*.xml");
  22. More AOP in Spring Part 2

    by , 11-16-2011 at 06:26 PM
    Along with aspect’s advice, pointcuts, which are used to pinpoint where an aspect’s advice should be applied, are considered among the most fundamental elements of an aspect. In Spring AOP, we define pointcuts using AspectJ’s pointcut expression language. If you’re already know AspectJ, then defining pointcuts in Spring should feel natural. Otherwise, I will provide you with quick tips that will help you with writing AspectJ-style pointcuts.

    In Spring AOP, the key thing to remember ...
    Tags: aop, spring aop Add / Edit Tags
  23. More on AOP in Spring Part 1

    by , 11-16-2011 at 06:03 PM
    In the previous tip, we started to look at Aspect Oriented Programming support in Spring. In this tip we will continue with this assessment of this exciting technology. Before we start there are a number of terms that I need to introduce to you in order that you can speak the lingo of AOP. I list these terms below:
    • Advice - I had briefly mentioned this is the last tip. Advice is the code to be executed at a join point that has been selected by a pointcut. Advice can execute before, after,

    Updated 11-16-2011 at 06:05 PM by Spring Framework

  24. An Introduction to Aspect Oriented Programming

    by , 11-16-2011 at 05:58 PM
    Aspect-oriented programming (AOP) is a new methodology that provides separation of crosscutting concerns. Crosscutting concerns are system wide concerns that span multiple modules. In applications, these system wide concerns would involve features such as logging, security, persistence and any other element common to a majority of the core business modules. This type of programming is done by introducing a new unit of modularization—an aspect—that crosscuts other modules. Using AOP you would implement ...
    Tags: aop, aspectj Add / Edit Tags
  25. Advanced Uses of SpEL

    by , 11-16-2011 at 05:50 PM
    There are a number of operations that you can apply on values as well as class scoped methods and constants using SpEL. Consider for example doing basic math operations on values. SpEL supports all the basic arithmetic operations that are supported in Java’s libraries. Consider that if you want to use constants or class-scoped methods you will need to use the T() operator. Below is an example of how it is to be used:


    The creates a Class object that ...
  26. More Simple SpEL Expressions

    by , 11-16-2011 at 05:48 PM
    Another basic thing that a SpEL expression can do is to reference another bean by its ID. With this functionality it is possible to use SpEL to wire one bean into another bean’s property by using the bean ID as the SpEL expression:

    Java Code:
    <property name="engine" value="#{hybrid}"/>
    As you can see, we’re using SpEL to wire the bean whose ID is "hybrid" into an engine property. Now it is possible to do this without SpEL, and you would get ...
  27. The Basics of SpEL

    by , 11-16-2011 at 05:44 PM
    The goal of a SpEL expression is to arrive at some value after evaluation. In the course of calculating that value, other values are considered and operated upon. The simplest kinds of values that SpEL can evaluate may be literal values, references to a bean’s properties, or perhaps a constant on some class.

    The most basic type of SpEL expression is one that contains only a literal value. A literal value would be a perfectly valid SpEL expression:

    Java Code:
    <property name="message"
  28. Introduction to Spring Expression Language

    by , 11-16-2011 at 05:42 PM
    Up until now, in the tips I’ve shown, all the constructor arguments and bean properties have been statically wired in the Spring configuration XML. What this means is that any definition you make has to be provided during development. This also applies to other beans which need to be defined statically in the Spring configuration.

    In Spring 3, the Spring Expression Language (SpEL) was introduced as a powerful and concise means of dynamically wiring values in constructor arguments ...
  29. Listening for Events in Spring

    by , 11-16-2011 at 05:39 PM
    During the application life cycle, the ApplicationContext will publish a handful of events that tell interested listeners what’s going on. All of these events are all subclasses of the abstract class org.springframework.context.ApplicationEvent. A couple of these subclasses are:
    • ContextClosedEvent—Published when the application context is closed
    • ContextRefreshedEvent—Published when the application context is initialized or refreshed
    • RequestHandledEvent—Published within a web application

    Updated 11-30-2011 at 05:12 AM by Spring Framework

    Application Context , Event
  30. Wiring Bean Post Processor in Spring

    by , 11-16-2011 at 05:32 PM
    In the last post, we create a class AnsiConvertor that implements the BeanPostProcessor interface. Now if we want to use this class in our application is running within a bean factory, we need to programmatically register the BeanPostProcessor using the factory’s addBeanPostProcessor() method:

    Java Code:
    BeanPostProcessor convertAnsi = new ConvertAnsi(); 
    Or if you decide to use an application context, you’ll need to register the ...
    Configuration , XML , Bean
Page 3 of 4 FirstFirst 1234 LastLast