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 0:00 the Dao layer, and 0:03 that is to create a service interface followed by an implementing class. 0:04 Let's first create the service package. 0:08 So right click on our main package, choose new package, and 0:10 name it service, and now the category service interface. 0:14 Right click on the service package, new class, drop this down to interface, and 0:19 we'll call it CategoryService. 0:24 Now this service will have the same four methods as the CategoryDao interface, so 0:29 I'll just copy them from the CategoryDao. 0:34 Go to CategoryDao, copy these, and paste them into 0:36 this interface, importing all necessary classes. 0:41 Cool. 0:48 Now the goal here is not to practice typing by creating needless duplication, 0:49 but rather to create neatly separated layers that can be maintained and 0:53 utilized as independently from one another as possible. 0:57 And though our service for categories contains the same methods as the Dao, 1:01 this will not be true for gifs and doesn't have to be true for categories either. 1:06 Just happens to work out that way in this project. 1:11 Okay, let's now code the implementation. 1:15 So we'll start by creating CategoryServiceImpl. 1:18 So right click service, choose Java Class. 1:21 I'll name it CategoryServiceImpl. 1:25 And I will say it implements CategoryService. 1:30 There it is. 1:37 And after you state that this class implements CategoryService, 1:38 you should immediately get an error since we're missing the implementations for 1:41 the interface methods. 1:45 Well, let's change that using an IDE option. 1:46 Implement all four methods, cool. 1:50 Since our service layer will call upon the Dao layer, 1:54 we need to autowire a CategoryDao here. 1:57 So we'll say Autowired, private CategoryDao, 2:01 and I'll name it the same thing categoryDao. 2:06 Notice I'm autowiring an object of type CategoryDao. 2:10 That's the interface type. 2:15 There we see what I was referring to earlier, 2:16 where we autowire something referenced by its interface name, and 2:19 Spring will find the only implementation of this interface that exists, 2:23 and that, for us, is the CategoryDaoImpl. 2:28 So an actual CategoryDaoImpl object will be autowired into this particular field. 2:32 Now focusing on the findAll method, 2:41 we can simply return the value given by the Dao's findAll method here. 2:42 So I can return the value that's returned from CategoryDao.findAll. 2:47 And our final order of business here is to annotate this class with the service 2:53 annotation. 2:58 So I'll stick that on the class. 2:59 And this is so that Spring can pick it up as a bean that can itself be 3:02 autowired as a CategoryService elsewhere, namely, 3:06 in our controllers, where we'll end up using it. 3:10 As a double reminder, these annotations of Spring components, 3:13 like the repository for the CategoryDaoImpl or the service here for 3:18 the CategoryServiceImpl, these annotations go on the implementations, 3:23 not on the interfaces themselves. 3:29
You need to sign up for Treehouse in order to download course files.Sign up