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.4 (2,477 ratings)
Instead of using a simple lifetime average, Udemy calculates a
course's star rating by considering a number of different factors
such as the number of ratings, the age of ratings, and the
likelihood of fraudulent ratings.
22,849 students enrolled Bestselling in Spring
57% off
Take This Course
  • Lectures 176
  • Length 28 hours
  • Skill Level Expert Level
  • Languages English
  • Includes Lifetime access
    30 day money back guarantee!
    Available on iOS and Android
    Certificate of Completion
Wishlisted Wishlist

How taking a course works


Find online courses made by experts from around the world.


Take your courses with you and learn anywhere, anytime.


Learn and practice real-world skills and achieve your goals.

About This Course

Published 7/2013 English

Course Description

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.

What are the requirements?

  • 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

What am I going to get from this course?

  • 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

Who is the target audience?

  • Java developers
  • Anyone with some fluency in Java and an interest in web or database programming

What you get with this course?

Not for you? No problem.
30 day money back guarantee.

Forever yours.
Lifetime access.

Learn on the go.
Desktop, iOS and Android.

Get rewarded.
Certificate of completion.


Section 1: Getting Started
In this video we'll start by discussing what you can use Spring for and what Spring actually is.
2 pages

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

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.
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.
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.
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.
We can load Spring XML files from anywhere on the classpath; we'll see how to do it here.
Section 2: Basic Bean Configuration
In this tutorial we'll take a look at how we can supply arguments to the constructors of our Spring beans via XML.
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.
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.
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.
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.
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
Yes, it sounds like something terrible from theoretical physics, but actually it's a simple way of setting bean properties with minimal typing
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.
What if you need to pass a bean a whole list of other beans? Once again it's quite simple.
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.
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.
Now it's time to look at setting a an arbitrary Map as the property of a bean.
Section 3: Autowiring
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
We can also autowire our Spring beans by matching their names or Ids to the names of set methods.
Spring lets us autowire constructors by matching types of beans to constructor parameters.
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.
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.
Section 4: Wiring with Annotations
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.
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 “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 …
The @Qualifier annotation enables us to further narrow the bean selection considered by Autowired.
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.
We can use some JSR-250 annotations to specify 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.
We can ditch the XML and tell Spring which classes are to be used to create beans just using annotations.
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.
Section 5: Spring Expression Language (SPEL)
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.
We can use SPEL with annotations too; the “Value” annotation will accept SPEL expressions.
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.
Section 6: Working with Databases
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.
Since we'll need to store database connection info in a config file, let's take a look at reading property files in Spring.
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.
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.
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.
We use various “JDBC Templates” in Spring to run SQL. In fact, we'll mostly get by using the simplest variety.
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.
Spring saves us from having to handle millions of JDBC exceptions; but we can still handle them when we want to.
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.
It's very easy to execute SQL update statements in Spring; in this tutorial we'll implement a “delete” method.
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.
For the sake of completeness, we'll add a “update” method to our DAO in this tutorial.
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.
Of course we can also do transactions in Spring, enabling us to run a bunch of statements in a failsafe manner.
Section 7: Web Application Basics with Spring MVC
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.
The next step is to add Maven support to our Eclipse project and bring in the necessary Spring jars.
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.
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.
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.
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.
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.
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.
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.
Now we can copy over our DAO code from previous tutorials and configure a context container (XML file) to create the necessary beans.

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.

Spring has a special tag which sets up a data source bean for us that will connect to our JNDI data source.
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.
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.
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.
Section 8: Working with Web Forms
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.
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.

Let's add a few CSS styles so that the form looks a little bit less ugly.
We can get our DispatcherServlet to serve static content, such as a CSS stylesheet or images.
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.
Let's finish adding validation tags to our form.
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.
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.
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.
Let's wire our database code into our controller now, so that our form actually does something useful.
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.
Section 9: Aspect-Oriented Programming (AOP)
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.
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 :)
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 ...
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.
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.
In this tutorial we'll see a new way of defining pointcuts, using “within” instead of “execution”.
We'll look at two more pointcuts designators in this tutorial; “this” and “target”.
There are a bunch of Point Cut Designators (PCDs) that are designed to target annotations; we'll explore them in this tutorial.
The Bean PCD lets you target beans, and supports asterisk wildcards.
“Args” lets you match methods based on argument types; it's fairly straightforward, although there are a few complications, as we'll see.
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.
We can also get method arguments using the binding form of “args”, which we'll take a look at here.
You can combine multiple pointcut designators in a single powerful expression.
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.

Students Who Viewed This Course Also Viewed

  • Loading
  • Loading
  • Loading

Instructor Biography

John Purcell, Software Development Trainer

After working as a software developer and contractor for over 14 years for a whole bunch of companies including CSC, Proquest, SPSS and AT&T in the UK and Netherlands, I decided to work full-time as a private software trainer. I now live in the beautiful city of Budapest, Hungary, from where I run the website Cave of Programming.

Ready to start learning?
Take This Course