The Spring framework , created by Rod Johnson, is an extremely powerful Inversion of control(IoC) framework to help decouple your project components’ dependencies.
In this series of tutorials, it’s provides many step by step examples and explanations on using the Spring framework.
New Spring 3.0 Tutorials (23/06/2011)
Added many Spring 3.0 tutorials on using Spring EL, JavaConfig, AspectJ and Spring Object/XML mapping (oxm). For what’s new in Spring 3.0, you can refer to this official Spring 3.0 references.
Spring Quick Start
Quick start to understand the basic of Spring framework development.
- Spring hello world example
Maven + Spring 2.5.6 hello world example. - Spring 3.0 hello world example (Spring 3.0)
Maven + Spring 3.0 hello world example, what are needed in new Spring 3.0 development. - Spring loosely coupled example
An example to show demonstrate how Spring make components loosely coupled.
Spring JavaConfig (Spring 3.0)
Spring 3.0 supported JavaConfig, now you can use annotations to do the configuration in Spring.
- Spring 3 JavaConfig examples
Demonstrate the use of @Configuration and @Bean to define bean in Spring - Spring 3 JavaConfig @Import example
Demonstrate the use of @Import to organize beans in modular.
Spring Dependency Injection (DI)
How Spring to do dependency Injection (DI) to manage object dependencies.
- Spring Dependency Injection (DI)
How Spring applies the Dependency Injection (DI) design pattern via Setter Injection and Constructor Injection. - Spring DI via setter method
Dependency injection a bean via setter method. - Spring DI via constructor
Dependency injection a bean via constructor. - Constructor injection type ambiguities in Spring
The constructor injection argument type ambiguity issue is always happening in a bean which contains multiple constructor methods with many arguments.
Bean Basic
All the classes you need to use in Spring Ioc container are considered “bean”, and declared in a Spring bean configuration file or via annotation.
- Spring bean reference example
How beans access to each other by specify the bean references in the same or different bean configuration file. - Inject value into bean properties in Spring
Three ways to inject value into bean properties. - Load multiple Spring bean configuration file
Developers always categorize different bean configuration files in different modules folder, here’s a tip to show you how to load multiple Spring bean configuration files. - Spring inner bean examples
Whenever a bean is used for one particular property only, it’s always advised to declare it as an inner bean. - Spring bean scopes examples
Bean scope is used to decide which type of bean instance should be return from the Spring container back to the caller. - Spring Collections (List, Set, Map, and Properties) example
Example to inject values into collections type (List, Set, Map, and Properties). - ListFactoryBean example
Create a concrete List collection class (ArrayList and LinkedList), and inject it into bean property. - SetFactoryBean example
Create a concrete Set collection class (HashSet and TreeSet), and inject it into bean property. - MapFactoryBean example
Create a concrete Map collection class (HashMap and TreeMap), and inject it into bean property. - Spring inject Date into bean property – CustomDateEditor
Normally, Spring is accepting date variable, here’s a tip to use CustomDateEditor to work around it. - Spring PropertyPlaceholderConfigurer example
Externalize the deployment details into a properties file, and access from a bean configuration file via a special format – ${variable}. - Spring bean configuration inheritance
Inheritance is very useful for a bean to share common values, properties or configuration. - Spring dependency checking
Spring comes with 4 dependency checking modes to make sure the required properties have been set in bean. - Spring dependency checking with @Required Annotation
Dependency checking in annotation mode. - Custom @Required-style annotation
Create a custom @Required-style annotation ,which is equivalent to @Required annotation. - Bean InitializingBean and DisposableBean example
Perform certain actions upon bean initialization and destruction. (interface) - Bean init-method and destroy-method example
Perform certain actions upon bean initialization and destruction. (XML) - Bean @PostConstruct and @PreDestroy example
Perform certain actions upon bean initialization and destruction. (Annotation) - Spring Profiles example
Spring @Profile allow developers to register beans by condition.
Spring Expression Language (Spring 3.0)
Spring 3.0 introduces features rich and powerful expression language known as Spring expression language, or Spring EL.
- Spring EL hello world example
Quick start to use the Spring expression language (EL). - Spring EL bean reference example
Reference bean, bean property using a dot (.) symbol. - Spring EL method invocation example
Call bean method in directly. - Spring EL operators example
Spring EL supports most of the standard relational, logical and mathematical operators. - Spring EL ternary operator (if-then-else) example
Conditional check , if else then. - Spring EL Arrays, Lists, Maps example
Works with Map and List. - Spring EL regular expression example
The regular expression to evaluate the condition. - Test Spring EL with ExpressionParser
Show you how to test Spring El easily.
Spring Auto Component Scanning
Spring is able to scan, detect and register your bean automatically.
- Spring Auto scanning components
Enable Spring to auto scan, detect and register your beans. - Spring Filter components in auto scanning
Example to filter certain components in auto scanning mode.
Spring AutoWiring Bean
Spring ‘auto-wiring’ modes to wire or beans automatically, both in XML and annotation.
- Spring Auto-Wiring Beans
Summary of 5 types auto wiring modes in Spring. - Spring Autowiring by Type
If the data type of a bean is compatible with the data type of other bean property, auto wire it. - Spring Autowiring by Name
If the name of a bean is same as the name of another bean property, auto wire it. - Spring Autowiring by Constructor
Actually, it’s autowiring by Type in constructor argument. - Spring Autowiring by AutoDetect
It means choosing “autowire by constructor” if default constructor is found, otherwise uses “autowire by type”. - Spring Autowiring with @Autowired annotation
Examples to show how to define ‘auto-wiring’ modes in annotation. - Spring Autowiring @Qualifier example
Example to identify which bean is qualified to autowired on a field.
Spring AOP (Aspect-oriented programming)
The Spring AOP modularize cross-cutting concerns in aspects. Put it simply, an interceptor to intercept some methods.
- Spring AOP Example – Advice
Examples and explanations about different types of Spring’s advices. - Spring AOP Example – Pointcut , Advisor
Examples and explanations about different types of Spring’s Pointcut and Advisor. - Spring AOP interceptor sequence
The sequence of the AOP interceptor will affect the functionality. - Auto proxy creator example
An auto proxy creator example to create proxy object for your beans automatically, useful to avoid creating many duplicated proxy objects.
Spring AOP + AspectJ framework
AspectJ supported since Spring 2.0, more flexible and powerful. However, this example is demonstrated in Spring 3.0.
- Spring AOP + AspectJ annotation example (Spring 3.0)
An example to show you how to integrate AspectJ annotation with Spring framework. - Spring AOP + AspectJ in XML configuration example (Spring 3.0)
Spring AOP with AspectJ in XML base configuration.
Spring Object/XML Mapper (Spring 3.0)
In Spring 3.0, Object to XML mapping (OXM) is moved from the Spring Web Services to the core Spring Framework.
- Spring Object/XML mapping example
Spring oxm + castor, convert Object to XML and vice verse.
Spring JDBC Support
Spring provides many helper classes to simplify the overall JDBC database operation.
- Spring + JDBC example
An example to show how to integrate Spring and JDBC. - JdbcTemplate + JdbcDaoSupport example
Example to use Spring’s JdbcTemplate and JdbcDaoSupport classes to simplify the overall JDBC database operation processes. - JdbcTemplate Querying examples
Here are a few examples to show how to use JdbcTemplate query() methods to query or extract data from database. - JdbcTemplate batchUpdate() example
A batchUpdate() example to show how to perform the batch insert operations. - SimpleJdbcTemplate Querying examples
More user friendly and simple way to query or extract data from database. - SimpleJdbcTemplate batchUpdate() example
Another batch update example with SimpleJdbcTemplate, a java5-friendly supplement to JdbcTemplate. - Named Parameters examples in SimpleJdbcTemplate
An example to show how to use named parameter as SQL parameter values, and this is only supported in SimpleJdbcTemplate. - JdbcTemplate queryForInt() is Deprecated
Spring Hibernate Support
Spring comes with many handy classes to support Hibernate ORM framework.
- Maven + Spring + Hibernate + MySql Example
A simple project of using Spring and Hibernate. - Maven + (Spring + Hibernate) Annotation + MySql Example
A simple project of using Spring and Hibernate (annotation version). - Spring AOP transaction management in Hibernate
An example to show how to manage the Hibernate transaction with Spring AOP. - Struts + Spring + Hibernate integration
Example to integrate Spring with Struts and Hibernate framework.
Spring E-mail Support
Spring’s provides MailSender to send email via JavaMail API.
- Sending E-mail via MailSender
Example to use Spring’s MailSender to send an email via Gmail SMTP server. - E-mail template in bean configuration file
Hard-code all the email properties and message content in the method body is not a good practice, you should consider define the email message template in the Spring’s bean configuration file. - Sending e-mail with attachment
Example to use Spring to send an email that has attachments.
Spring Scheduling Support
Spring has very good support in both JDK timer and Quartz framework.
- Spring + JDK Timer scheduler example
An article about how Spring schedules a job with JDK timer. - Spring + Quartz scheduler example
An article about how Spring schedules a job with Quartz framework. - Spring + Struts + Quartz scheduler example
Integrate Spring with Struts and schedule a job with Quartz framework.
Spring Caching Support
Integrating Spring with Other Web Frameworks
Spring integrates with other web frameworks.
- Spring dependency injection in the servlet session listener
Spring comes with a “ContextLoaderListener” listener as a generic way to enable the Spring dependency injection in session listener and almost all other web framework. - Struts + Spring integration
Example to integrate Spring with Struts 1.x framework. - Struts 2 + Spring integration example
Example to integrate Spring with Struts 2 framework. - JSF 2.0 + Spring integration example
Example to integrate JSF 2.0 with Spring framework. - JSF 2.0 + Spring + Hibernate integration example
Example to integrate JSF 2.0 + Spring + Hibernate framework together. - Wicket + Spring integration example
Example to integrate Wicket with Spring framework. - Struts 2 + Spring + Quartz scheduler integration example
Example to integrate Spring + Struts 2 + Quartz. - Struts 2 + Spring + Hibernate integration example
Example to integrate Spring + Struts 2 + Hibernate. - RESTEasy + Spring integration example
Examples to inject Spring beans into JBoss RESTEasy
Spring FAQs
- Install Spring IDE in Eclipse
An article about how to install Spring IDE in Eclipse. - Resource bundle with ResourceBundleMessageSource example
ResourceBundleMessageSource is the most common class to resolve text messages for different locales. - Access MessageSource in bean (MessageSourceAware)
An example to show how to get the MessageSource in a bean via MessageSourceAware interface. - Resource loader with getResource() example
Spring’s resource loader provides a very generic getResource() method to get the resources like (text file, media file, image file…) from the file system , classpath or URL.
Spring Common Errors
Some Spring common error messages.
- ClassNotFoundException : org.springframework.web.context.ContextLoaderListener
- Cannot proxy target class because CGLIB2 is not available
- CGLIB is required to process @Configuration classes
- java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLException
- java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSerializer
- ERROR Source or Target option 1.5 is no longer supported. Use 1.6 or later
- [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher
Spring Knowledge
- Spring @PropertySource example
- Spring @Value default value
- Spring 3 and JSR-330 @Inject and @Named example