Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Writing Your First Service3:32 with Chris Ramacciotti
In this video we start coding our service layer, which will facilitate communication between our controllers and DAOs.
Git Command to Sync Your Code to the Start of this Video
git checkout -f s2v4
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone email@example.com:treehouse/giflib-hibernate.git
To update your local repository to match a video's starting point, you can use the
git checkout command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
In creating our service layer, we'll use the same approach as we did for
the Dao layer, and
that is to create a service interface followed by an implementing class.
Let's first create the service package.
So right click on our main package, choose new package, and
name it service, and now the category service interface.
Right click on the service package, new class, drop this down to interface, and
we'll call it CategoryService.
Now this service will have the same four methods as the CategoryDao interface, so
I'll just copy them from the CategoryDao.
Go to CategoryDao, copy these, and paste them into
this interface, importing all necessary classes.
Now the goal here is not to practice typing by creating needless duplication,
but rather to create neatly separated layers that can be maintained and
utilized as independently from one another as possible.
And though our service for categories contains the same methods as the Dao,
this will not be true for gifs and doesn't have to be true for categories either.
Just happens to work out that way in this project.
Okay, let's now code the implementation.
So we'll start by creating CategoryServiceImpl.
So right click service, choose Java Class.
I'll name it CategoryServiceImpl.
And I will say it implements CategoryService.
There it is.
And after you state that this class implements CategoryService,
you should immediately get an error since we're missing the implementations for
the interface methods.
Well, let's change that using an IDE option.
Implement all four methods, cool.
Since our service layer will call upon the Dao layer,
we need to autowire a CategoryDao here.
So we'll say Autowired, private CategoryDao,
and I'll name it the same thing categoryDao.
Notice I'm autowiring an object of type CategoryDao.
That's the interface type.
There we see what I was referring to earlier,
where we autowire something referenced by its interface name, and
Spring will find the only implementation of this interface that exists,
and that, for us, is the CategoryDaoImpl.
So an actual CategoryDaoImpl object will be autowired into this particular field.
Now focusing on the findAll method,
we can simply return the value given by the Dao's findAll method here.
So I can return the value that's returned from CategoryDao.findAll.
And our final order of business here is to annotate this class with the service
So I'll stick that on the class.
And this is so that Spring can pick it up as a bean that can itself be
autowired as a CategoryService elsewhere, namely,
in our controllers, where we'll end up using it.
As a double reminder, these annotations of Spring components,
like the repository for the CategoryDaoImpl or the service here for
the CategoryServiceImpl, these annotations go on the implementations,
not on the interfaces themselves.
You need to sign up for Treehouse in order to download course files.Sign up