The Java Spring Tutorial: Learn Java's Popular Web Framework
4.1 (4,278 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
31,339 students enrolled

The Java Spring Tutorial: Learn Java's Popular Web Framework

Learn the hottest, most in-demand Java web framework, including web programming with Spring MVC and Hibernate. Lifetime access with no subscription on Udemy.
4.1 (4,278 ratings)
Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.
31,337 students enrolled
Created by John Purcell
Last updated 1/2016
Current price: $23.99 Original price: $34.99 Discount: 31% off
5 hours left at this price!
30-Day Money-Back Guarantee
This course includes
  • 28 hours on-demand video
  • 4 articles
  • 167 downloadable resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
Training 5 or more people?

Get your team access to 4,000+ top Udemy courses anytime, anywhere.

Try Udemy for Business
What you'll learn
  • Understand the fundamentals of the Java Spring framework
  • Learn how to develop web and database applications with Spring
  • Understand the basics of Hibernate for SQL-free database connectivity
Course content
Expand all 175 lectures 27:47:08
+ Getting Started
7 lectures 01:11:02
In this video we'll start by discussing what you can use Spring for and what Spring actually is.
Preview 06:30

Please see the attached PDF if you want to use Spring 4.2+ for this course.

Preview 2 pages
How to set up your system for Spring development, installing Java, Eclipse EE, Maven, the m2e Maven Eclipse plug, the Spring IDE Eclipse plugin, and optionally Tomcat if you want to get into web development.
Preview 15:54
In this video we'll take a look at Maven; what it is, basic usage, and how to create a basic Maven project. We'll only need a basic knowledge of Maven to do everything we want to do later on with Spring, in fact. This tutorial assumes you've installed the Maven Integration for Eclipse plugin as described in the previous tutorial, and that you have a connection to the Internet.
Preview 13:31
A tutorial on creating Maven projects from the command line. This is an optional video in this course; we won't actually need to use Maven from the command line, but seeing what's possible using “pure” Maven will help you to understand Maven, and for that reason I've created this tutorial.
Preview 08:45
Creating a simple “hello world” project in Spring. Yes, finally it's time to create an actual Spring project, now that we've got our system set up.
Preview 21:10
We can load Spring XML files from anywhere on the classpath; we'll see how to do it here.
Preview 05:12
+ Basic Bean Configuration
12 lectures 01:23:41
In this tutorial we'll take a look at how we can supply arguments to the constructors of our Spring beans via XML.
Preview 08:16
Of course it's possible to set properties of beans in Spring from XML; that is, we can invoke set methods of our objects and pass values to them which we code into the XML.
Preview 07:16
Dependency Injection is one of the buzzwords for which Spring is famous. It sounds ferocious, but it isn't at all complicated. Here we'll take a look at how we can inject a Spring bean with another bean, allowing you to configure your beans with whatever services or facilities take your fancy – provided you've got the functionality you need in another bean somewhere, of course.
Preview 11:39
Beans have a scope; for our purposes here, the choice we have is between reusing our one and only bean or creating new beans to order.
Bean Scope
Beans in Spring have a distinct lifecycle, and we can make code run when beans are created or destroyed. Finally a reliable “destructor” in Java! Well, sort of.
Init and Destroy Methods
Sometimes you want to create objects via a factory method or class rather than via a public constructor. Spring makes it easy to do this through your application contexts
Factory Beans and Methods
Yes, it sounds like something terrible from theoretical physics, but actually it's a simple way of setting bean properties with minimal typing
The P Namespace
If you need to set a bean property that's actually a list or set of stuff from XML, you can do this too quite easily.
Setting List Properties
What if you need to pass a bean a whole list of other beans? Once again it's quite simple.
Lists of Beans
Sometimes you want to define a bean solely for the purpose of passing it to another bean, and you never want to refer to it in your XML again. In that case, maybe you need an inner bean.
Inner Beans
There's a class in Java called “Properties”, which is basically a kind of Map. In Spring, whenever a bean property expects to be passed a Map, and the keys and values are both Strings, we can use a property map to supply the required map. Don't get confused between a property map, and “properties” meaning the fields of objects (beans). I noticed after making this tutorial that I had initialised my Map property to a new HashMap, which is kind of redundant since Spring will create a new Map; although it does at least allow us to run the toString() method of our bean when it has not had its property map set.
Property Maps
Now it's time to look at setting a an arbitrary Map as the property of a bean.
Arbitrary Maps as Bean Properties
+ Autowiring
5 lectures 31:12
So far we've been wiring our beans manually, but it's also possible to let Spring wire a lot of stuff up automatically. Whether that's necessarily a good idea in a given situation, is another matter. We'll take a look at how to wire stuff up by matching property types to bean types in this tutorial
Autowiring by Type
We can also autowire our Spring beans by matching their names or Ids to the names of set methods.
Autowiring by Name
Spring lets us autowire constructors by matching types of beans to constructor parameters.
Autowiring by Constructor
What if we want to set a default autowiring method for an entire XML container? It's quite easy, and might save us a lot of typing – but be careful to avoid ambiguity and make sure you have all the right beans defined. Autowiring must always be used judiciously.
Default Autowiring
Spring has a trick or two for removing autowire ambiguities. We can remove beans from our list of possible candidates to autowire into other beans, or we can specify certain beans as primary autowire candidates, to be considered first.
Removing Autowire Ambiguities
+ Wiring with Annotations
9 lectures 43:57
XML isn't the only way to configure Spring; these days we can also wire using annotations. In this tutorial we'll adding some stuff to our XML to support annotation-based autowiring.
Adding Support for Annotation-Based Wiring
The most basic wiring annotation is perhaps the Autowired annotation. We can add this annotation in various locations to our Java code to get Spring to wire up our beans.
The "Autowired" Annotation
The “required” attribute on @Autowired enables us to specify optional beans. I was a little confused in this video over why Autowired sometimes tolerated ambiguity; now I think a large part of what was happening was that Autowired just decides not to consider a bean a candidate when it's used it once already! One lives and learns …
Optional Beans
The @Qualifier annotation enables us to further narrow the bean selection considered by Autowired.
Using Qualifiers
Back in 2006, another annotation was introduced into Java to support dependency injection. We can use it quite happily in Spring, as an alternative to Spring's native Autowired. If you have problems using @Resource, check your project properties and ensure you're building with JDK 1.6 or later.
The Resource Annotation (JSR-250)
We can use some JSR-250 annotations to specify init and destroy methods.
Annotation-Based Init and Destroy Methods
Winding forwards to 2009, JSR-330 introduced yet more annotations; @Inject is now one of Java's standard dependency-injection annotations, and of course we can use it in Spring. If you can't find @Inject, make sure you've added the right jar (you can add it via Maven) and ensure that your project is set to build with Java 1.6 or later.
The Inject annotation (JSR-330)
We can ditch the XML and tell Spring which classes are to be used to create beans just using annotations.
Automatic Bean Discovery
We can use the @Value annotation to supply values for properties. Later on we'll take a look at SPEL, which will make this a whole lot more interesting.
Setting Property Values via Annotations
+ Spring Expression Language (SPEL)
3 lectures 26:14
Spring Expression Language makes property values dynamic, allowing us to run code and figure stuff out when we're busy wiring our beans. We'll also take a look at the “safe navigation” operator.
Introducing SPEL
We can use SPEL with annotations too; the “Value” annotation will accept SPEL expressions.
Using SPEL with Annotations
In this tutorial we'll take a look at some useful operators that we can use in SPEL to create new objects, access static methods and perform arithmetical and logical operations.
Some useful SPEL Operators
+ Working with Databases
14 lectures 01:59:32
A brief review of how to create a database with MySQL. I won't cover MySQL or SQL in any depth in this tutorial series, but we'll go over the basics.
Creating a Database with MySQL
Since we'll need to store database connection info in a config file, let's take a look at reading property files in Spring.
Using Property Files
We'll use the DAO pattern to work with data retrieved from the database in Spring; in this tutorial we'll create a couple of objects that we'll be using a little later on.
Implementing the DAO Pattern
We'll need a JDBC connector jar to connect to our database. If you're using an open source database like MySQL, you can probably do this via Maven. Otherwise you'll have to do a little Googling for the right jar.
Downloading a Connector Jar
Usually we use connection pools in Spring when we connect to databases. Later on, when we look at Spring MVC for web applications, we'll use connection pools provided by our application server. Since we're just creating a plan Java app here, we'll need to use an external (free) technology to pool our database connections.
Configuring Connection Pooling with Apache DBCP
We use various “JDBC Templates” in Spring to run SQL. In fact, we'll mostly get by using the simplest variety.
JDBC Templates
Finally it's time to do a SQL query on our database. We'll use our JDBC Template to set the properties of a list of DAO beans, corresponding to the rows in our table.
Querying the Database
Spring saves us from having to handle millions of JDBC exceptions; but we can still handle them when we want to.
Database Exceptions
While JDBC allows us to use indispensable placeholders in SQL, Spring goes further and lets us used named placeholders, reducing the possibility of bugs and confusion.
Named Parameters
It's very easy to execute SQL update statements in Spring; in this tutorial we'll implement a “delete” method.
Update Statements
In this tutorial we'll add a “create” method to our DAO; we'll also see a very nice way of getting values for placeholders directly from your beans.
Getting Placeholder Values from Beans
For the sake of completeness, we'll add a “update” method to our DAO in this tutorial.
Adding an Update Method to the DAO
The Spring equivalent of a prepared statement is a batch update. We'll look at running SQL queries with multiple different placeholder values in this tutorial.
Batch Updates: Prepared Statements
Of course we can also do transactions in Spring, enabling us to run a bunch of statements in a failsafe manner.
+ Web Application Basics with Spring MVC
15 lectures 02:29:42
We'll start our foray into web apps by creating a non-Spring web app; then we'll Springify it later on. You'll need either Eclipse EE or Eclipse Java IDE + WTP Plugin to get this working, along with a Java application server such as Tomcat.
A Basic Non-Spring Web App
The next step is to add Maven support to our Eclipse project and bring in the necessary Spring jars.
Bringing in Maven
The first step towards turning our web app into a Spring web app is to add a dispatcher servlet; this is the Spring servlet that will handle all our requests within the Spring MVC architecture.
The Dispatcher Servlet
After hitting the dispatcher servlet, requests are routed to a Spring controller in a Spring MVC web app. Here we'll add a single controller.
Adding a Controller
Finally we can get our Spring web app actually working. In this tutorial we'll add a view resolver that will tell the dispatcher servlet how to interpret the logical view name sent back from our controller.
View Resolvers
We need to figure out some kind of a way of passing data from our controller to our view, using the soon-to-be-added data model. We'll make a start on it here by looking at how Spring lets us add data to the session.
Adding Data to the Session
Spring has a bunch of easy-to-use data model classes that will make passing data from the controller to the view code quite simple. We'll take a look at a couple of possibilities in this tutorial.
Using Spring Data Models
In this tutorial we'll add JSTL support to our application. JSTL lets us use special tags in our HTML to output data, create loops, output stuff conditionally and do lots of other useful stuff, all without mixing up our view code with Java implementation code.
Using JSTL (JSP Standard Tag Library)
Java application servers like Tomcat provide us with a way of setting up connection pooling; the precise method you need to use to set up and connect to your connection pool depends on your server and database, and your hosting service. Here we'll look at setting up a JNDI data source for Tomcat 7 and MySQL.
Configuring a JNDI Data Source
Now we can copy over our DAO code from previous tutorials and configure a context container (XML file) to create the necessary beans.
Bringing in the DAO Code

We can use a special Spring class to load extra XML files. Our configuration for this project is actually going to be split among multiple XML files, which we'll load at startup.

Loading Bean Containers with ContextLoaderListener
Spring has a special tag which sets up a data source bean for us that will connect to our JNDI data source.
Creating a Datasource Bean
We're almost ready to display our data via Spring, but first we need a service layer. Our “service” class will simple be a class that's responsible for organising and using our DAOs, instead of cluttering up our controllers with this code.
Adding a Service Layer
Just a bit of reorganization in this tutorial. We'll add a second controller so that we can have a separate home controller and offer-related controller.
Adding a New Controller
Spring makes it very easy to get URL parameters. We'll take a look at how to do it here, because it'll be good preparation for getting the results of form submissions later.
Getting URL Parameters
+ Working with Web Forms
11 lectures 01:54:22
We'll do a little bit of config in this tutorial, followed by creating an HTML form that we can use to create new “offers” in our database.
Creating a Form
It's really easy to get the values that the user has entered in a form. Amazingly easy!

Also, Yavuz Yurtbeğendi tells me that you might need the attached XML fragment if you have problems with unicode characters.

Getting Form Values
Let's add a few CSS styles so that the form looks a little bit less ugly.
Adding CSS Styles
We can get our DispatcherServlet to serve static content, such as a CSS stylesheet or images.
Serving Static Resources
By making use of some Hibernate stuff, we can get Spring to validate our form. We'll start by adding some simple validation to one form field.
Adding Hibernate Form Validation Support
Let's finish adding validation tags to our form.
More Form Validation Tags
We can easily make our forms redisplay entered values when validation fails, so that the user is not forced to enter everything all over again. To do this, we'll use some special Spring form tags.
Making Forms Remember Values
It's important to tell the user why a form failed to validate; nothing is worse than not knowing why a form has failed to validate. So let's add some validation error messages to our form.
Displaying Form Validation Errors
You may never feel the need to create your own custom form validation constraint annotation; after all, regular expressions give you a lot of flexibility. But just in case you want to know how to make a totally new constraint annotation, here's the lowdown.
Creating a Custom Validation Annotation
Let's wire our database code into our controller now, so that our form actually does something useful.
Hooking Up the Controller and Database Code
What if our database code throw an exception? Spring makes it easy to handle whichever exceptions you feel like handling. In this tutorial we'll add a page that appears when any database exception is thrown.
Exception Handling in Spring MVC
+ Aspect-Oriented Programming (AOP)
15 lectures 02:16:19
We'll create a new project here to experiment with; there's no new Spring stuff in this tutorial, except that we'll add a new dependency, spring-aspects.
A Base Project for Working with Aspects
Now we can turn our Logger bean from the last tutorial into an “aspect”, containing “advice” which is run at a “join point” specified by a “pointcut”. Phew :)
A Simple Aspect Example
Annotations being all the rage in Spring these days, let's see how you can do aspect-oriented programming using aspects. As we'll see there are pros and cons to using annotations here, but we'll stick with them in these videos because they at least avoid us having to do loads of XML configuration and take us back towards real programming ...
Annotation-Based Aspects
Wildcards in Pointcut Expressions
“Before” advice isn't the only kind of advice we can use. We'll take a look at an arrange of advice types in this tutorial.
Advice Types: After, Around and Others
It's important to understand the relationship between Spring's proxy objects and the target objects they represent. Otherwise, you'll be prone to casting beans to the wrong type. We'll delve into the mystery and figure out how to avoid problems in this tutorial.
Proxies, Interfaces and Aspects
In this tutorial we'll see a new way of defining pointcuts, using “within” instead of “execution”.
The “Within” Pointcut Designator
We'll look at two more pointcuts designators in this tutorial; “this” and “target”.
“This”, “Target” and Matching Subpackages
There are a bunch of Point Cut Designators (PCDs) that are designed to target annotations; we'll explore them in this tutorial.
Annotation-Specific PCDs
The Bean PCD lets you target beans, and supports asterisk wildcards.
The “Bean” PCD
“Args” lets you match methods based on argument types; it's fairly straightforward, although there are a few complications, as we'll see.
The “Args” PCD
We can get the arguments being passed to our advised methods; there are two good ways to do it, and we'll look at the simplest in this tutorial.
Getting Target Method Arguments
We can also get method arguments using the binding form of “args”, which we'll take a look at here.
Getting Arguments Using “Args”
You can combine multiple pointcut designators in a single powerful expression.
Combining Pointcuts
Aspects allow you to add extra functionality in to your bean proxies. We'll take a look at adding some simple dummy extra functionality to our beans in this tutorial.
Introductions: Adding Functionality Using Aspects
+ Spring Security and Managing Users
23 lectures 04:34:08
Let's start by reviewing what a “filter” is and how it works.
Servlets Filters: A Review
We'll start adding security to our website by adding a Spring filter that will monitor all incoming requests.
Adding a Spring Security Filter
Now we can set up a Spring default login form, allowing people in only when they type the correct user name and password.
Adding a Spring Login Form
Using what we've already learned, we can easily add some access rules to serve static content such as CSS files and images.
Serving Static Resources: Access Rules
We can supply our own login form to replace the default Spring one, which we can then customise to our hearts' content.
Customising the Login Form
If the user types an incorrect username or password, it'd be good to tell them so. We'll add a login error message to our form in this tutorial.
Displaying Login Errors
So far we've been hardcoding user names and passwords in our XML config file, but it'd be much better to start this information in a database. One possibility is to use an LDAP database, but here's we'll make use of our existing MySQL database (or we could use any relational database) to store user names and passwords.
Authorising Users from a Database
Let's add a form that allows people to create new users, in other words, to register on our website.
Adding a “Create Account” Form
No new Spring stuff in this tutorial, but we'll add database-related code to made the user creation form work.
Making the “Create Account” Form Work
We'll add validation to “User” in this tutorial; once again, no new Spring stuff, but we'll apply stuff we've previously learned.
Adding Validation to the User Form

We need to stop users creating multiple accounts with the same user name; actually they already can't since this would violate a database constraint, but we need to catch the error in a more elegant manner.

Dealing with Duplicate Usernames
We've been hardcoding messages in our Java so far, which is a terrible idea really. Let's move them all into a property file, making it much easier to maintain and alter the messages -- especially if we want to create version of the website in other languages later on.
Storing Validation Messages in a Property File
Wandering away from Spring slightly, let's use the JQuery javascript library to add some client-side validation to check that the confirmed password matches the actual password.
Using JQuery to verify the password
We can use some standard JSTL to get error messages from our property file into our Javascript validation code.
Using Property File Values in JSPs
Since the user can log in, it'd be nice if they can log out too. We'll add such a link in this tutorial.
Adding a Logout Link
Spring allows us to define “roles” for our users, which determine what they can do. We'll create a new role in this tutorial, allowing privileged users to do some stuff that other users can't.
Working With Roles
We can output certain links or text on our web pages based on whether the user is logged in or has certain roles or not.
Outputting Text Based on Authentication Status
Here's a class I really should have mentioned earlier; BeanPropertyRowMapper lets us map queries to beans with a minimum of fuss.
Row Mapping with BeanPropertyRowMapper
We can use custom SQL to query our user database, allowing us to structure our user database how we want. In this tutorial we'll use it to fix a little problem with the application; user names are not currently case-sensitive, and probably should be.
Using Custom Authentication Queries: Case Sensitive Usernames
Spring allows us to secure individual methods against unauthorised access; implemented behind the scenes via aspects, this gives us a level of security that's even stronger than url-based security.
Method-Level Access Control
Spring actually throws an exception if an unauthorised user tries to run a protected method; we can catch the exception with our global handler and redirect to a page of our choice.
Catching Secure Annotation Violations
We can add a checkbox to our login form that will keep the user logged in for a specified amount of time; this is known as “remember me” functionality. Spring makes it very easy to add such a checkbox!
Adding “Remember Me” Functionality
Storing passwords unencrypted in the database is a very bad idea, since anyone who has access to the database knows the users' passwords. Much better to encrypt them first, and only store the encrypted version.
Encrypting Passwords
  • Fair knowledge of the Java programming language
  • Knowledge of HTML, servlets and JSPs is recommended for the web section of this tutorial
  • Some acquaintance with XML is helpful
  • Knowledge of SQL and databases is helpful for the database-related parts of this tutorial

An introduction to the widely-used Java Spring framework. Discover how to wire together your Java objects using Spring and dependency injection. You'll learn how to set up your system for Spring development, how to use Maven, and how to work with databases using Spring and Hibernate and how to create web applications with Spring MVC. We'll also look at managing user accounts with Spring Security,JDBC, working with web forms, Apache tiles for building modular web pages, aspect-oriented programming (AOP) and using Log4J and JUnit.

Who this course is for:
  • Java developers
  • Anyone with some fluency in Java and an interest in web or database programming