Create a Data Repository Component10:07 with Chris Ramacciotti
Modeling each GIF with a POJO is an essential step toward storing all our GIF data. In this video, we'll combine all those GIF objects into a single collection and create one method for accessing those GIF objects from the collection.
Code Block for GIF Data
Copy and paste the code below into your
private static final List<Gif> ALL_GIFS = Arrays.asList( new Gif("android-explosion", LocalDate.of(2015,2,13), "Chris Ramacciotti", false), new Gif("ben-and-mike", LocalDate.of(2015,10,30), "Ben Jakuben", true), new Gif("book-dominos", LocalDate.of(2015,9,15), "Craig Dennis", false), new Gif("compiler-bot", LocalDate.of(2015,2,13), "Ada Lovelace", true), new Gif("cowboy-coder", LocalDate.of(2015,2,13), "Grace Hopper", false), new Gif("infinite-andrew", LocalDate.of(2015,8,23), "Marissa Mayer", true) );
So now you've had a chance to see the full flow of data from the storage of GIF 0:01 details in a GIF object to the adding of a GIF object to a model map and 0:04 finally the access of that object from a timely template for display. 0:09 There's one problem with our GIF detail page at this point and 0:13 that is it's only able to display the details of one hard coded GIF. 0:16 The compiler bot. 0:20 But what if we want the controller method and 0:22 template to control the display of any GIF object according to the URL requested? 0:24 For that, we'll need to step up our game. 0:29 In particular, we'll need to store a collection of GIF objects in memory 0:33 that our application can choose from. 0:36 Next, we'll be adding what's called a repository of GIF objects. 0:39 A repository is essentially a collection that we've stored somewhere in memory or 0:44 in a database. 0:48 For this simpler application, we'll store it in memory using a standard Java list. 0:49 Let's get started. 0:54 Before we make our GIF repository, I'm going to create a package that would hold 0:56 any data repositories we might want to include in our application. 1:00 So I'm going to right-click on the giflib package, and 1:03 choose New, Package, and call it data. 1:07 In this package is where I'll put the GIF repository, 1:12 which will be a new Java class named, not surprisingly, GifRepository. 1:15 This class will act as both our storage device for 1:24 GIF objects as well as methods for interacting with those GIF objects. 1:26 In a future course we'll show you how to shift your storage mechanism to a database 1:31 but that's a bit outside the scope of the current course. 1:35 So for now we'll stick with storing our GIF objects into a static Java list. 1:37 To begin, I'll create that list as a field. 1:42 Make sure to import java.util.List and 1:47 com.teamtreehouse.giflib.model.gif. 1:51 And I'll call this ALL_GIFS. 1:56 Notice I'm using the all caps naming convention because this 1:59 is a class constant. 2:02 In this course, we're going to predefine our GIFs. 2:04 Though in a production application, again, this would likely come from a database. 2:07 For this I'm going to use a convenience method offered by the arrays class called 2:12 asList. 2:16 In to it I can pass any number of GIF objects. 2:17 So arrays, it comes from the java.util package, .asList. 2:24 This is, as opposed to constructing a new array list, 2:32 then using the add method a bunch of times to add individual objects. 2:35 I'll include six GIF objects here, one for 2:40 each image I've given you in the GIFS directory. 2:43 In the teacher's notes I've given you this block of code 2:46 in case you wanna save yourself some typing. 2:48 I'll go ahead and copy and paste that now so 2:51 that you don't have to watch me type all that code. 2:53 Give myself some space there. 2:56 Copy that I will get back in here and I will paste that right inside there. 2:58 Now, there's one import I need to resolve and that's of the local date class. 3:03 So I'll hit option enter in Inteli J its automatically take its first suggestion. 3:07 Might move this back one tab as well. 3:13 Cool, and that's our storage device. 3:16 I simple JAVA list. 3:18 But I also mentioned that the beyond storing our data, 3:20 this class would offer us methods for retrieving that data. 3:23 Because this list is private, we'll need to add some public 3:27 methods in order to access this list outside of the class. 3:30 Let's add one of those methods now. 3:35 We'll make a method that allows us to retrieve a GIF object with a given name. 3:37 So it will return a GIF object. 3:42 And I'm gonna call it findByName. 3:45 I'll give it a String parameter, so that the collar of this method can 3:49 pass at the name to retrieve the GIF object associated with that name. 3:54 So inside this method, I'm going to iterate over 3:59 all of my GIF objects in the list, so I'll use this enhanced for loop. 4:02 Oops, I forgot the name of my variable here. 4:07 Gif in ALL_GIFS, so it's a for 4:10 each loop or enhanced for loop for every GIF object. 4:15 In all GIFS I want to do the following. 4:18 If the particular GIF object I'm looking at has a name that equals the name 4:22 that was passed in, well then what I wanna do is return exactly that GIF object. 4:29 Now, if I get through the entire list without ever finding a GIF 4:34 matching this name, then what I want to do is return null. 4:38 We'll leverage this method for 4:45 our GIF controller class, which I'll switch to now. 4:46 We'll want to leverage that method we just wrote from the GIF details method here so 4:52 that we can feed the GIF details template with any GIF object instead of 4:56 this one that we've hard coded. 5:00 In order to have access to the GIF repository we'll need a reference to a GIF 5:02 repository object, since we'll likely access this from any controller method 5:07 I'll add it as an instance field. 5:11 And we'll do that up top. 5:13 Private GifRepository and 5:14 I'll just call it GifRepository. 5:18 Of course, without assigning anything to GifRepository, 5:23 if we try to call any of its methods like the find my name 5:26 method in any of our controller methods, we'll fall on our faces. 5:30 Because the field will be null and 5:33 we'll encounter a null pointer exception as I'm sure you've seen many times before. 5:35 There are a couple of ways we could go about initializing this GifRepository 5:41 field. 5:45 One way would be to use a constructor in this class. 5:46 And when the constructor is called we'd assign the GifRepository 5:49 field a new instance of a GifRepository class. 5:53 But there's actually another feature in spring that we can leverage here to 5:57 reduce our coding. 6:00 It turns out that Spring can initialize fields for 6:02 us as long as it can find a spring component of the same class for the field. 6:04 In our case it needs a spring component for GifRepository. 6:10 To tell spring that we want it to auto assign our field, 6:13 we use the autowired annotation. 6:17 This tells Spring to construct and 6:20 assign a GifRepository object to this instance field as soon as it's needed. 6:22 The next question is, how will Spring know that 6:28 the GifRepository class is a valid Spring component, as I mentioned? 6:30 Here's a surprise, we'll use an annotation. 6:36 Remember how we annotated our AppConfig class with ComponentScan? 6:39 Because of this, Spring will scan that same package and all of it's sub packages. 6:44 And consider all classes annotated with component as valid 6:49 options depending on the class name. 6:53 So all we need to do is switch to the GifRepository 6:57 class and add the component annotation. 7:02 So that's applied at the class level and it also comes from the Spring framework. 7:05 There we go. 7:11 Now, Spring will pick this up during the initial component scan as a result of our 7:12 app config being loaded. 7:17 And when the GIF controller needs a GifRepository class, 7:19 Spring will create one for us or use one that's already been created. 7:22 The process of this runtime creation of objects that is the injection 7:27 using the Autowired annotation. 7:32 This process without explicitly writing code to construct and 7:34 assign objects is called dependency injection or DI for short. 7:38 That is, we didn't ever write code that calls the GifRepository constructor and 7:44 assign the resulting object to our GifRepository field. 7:49 Instead, we let Spring wire our objects together with Autowire. 7:53 As you continue your study of Java development, and 7:58 especially with Spring, you'll be seeing much more DI. 8:01 It's one of Spring's most powerful and core features. 8:05 Now, back to our gifDetail's controller method. 8:08 What we can do at this point is replace the creation of our GIF object with a call 8:11 to the findByName method of the GifRepository field. 8:15 So instead of creating a new object here, I will call on a gifRepository object, 8:19 the findByName method. 8:24 And to it we'll pass the name of the GIF that we'd like 8:28 to grab from our gifRepository. 8:31 Here, I'll use android-explosion. 8:34 Forget my semi colon there. 8:38 While this replacement will produce the same result, it represents 8:41 a giant step in the direction of creating a dynamic detail page. 8:44 Let's reboot and redeploy our app to make sure that we haven't broken everything. 8:49 So I'll start my boot run task, and 8:53 remember it could take a moment, you start it too soon you might see an error. 8:55 Sometimes it's definitely safe to wait for 9:00 that message in your console saying that the task has been complete. 9:02 So I'll click bootRun again, And 9:08 after I see all my output come through and see that Tomcat has started, I will switch 9:13 back to my browser, and I'll refresh this to make sure nothing is yet broken. 9:18 Excellent. 9:24 All is well. 9:25 It looks like our web app is still intact from the user's standpoint, so 9:27 we're in great shape! 9:30 Well, that was quite a bit of information in coding. 9:32 It may seem like a lot to digest right now, so 9:35 if you're feeling a bit overwhelmed don't panic. 9:37 You can always go back in the video to re-watch the parts that are still unclear. 9:40 Or you can come back to them after you finish the course. 9:45 Also, as you're coding the application on your own machine, 9:48 if you run into any problems, please use the forum to post your questions, and 9:52 include your code as necessary. 9:56 Let's take a break now before finishing the task of making our detail page, 9:59 present the GIF details given by a specific GIF's URI. 10:03
You need to sign up for Treehouse in order to download course files.Sign up