Adding a Spring Configuration File5:16 with Chris Ramacciotti
In this video, we'll add the minimum required configuration details for Spring, so that upon launching our application, all components are properly discovered.
Localhost is the alias or hostname that maps to the IP address of your own local machine. That IP address is 127.0.0.1.
Configuration with JavaConfig or XML
If you want to explore alternate forms of configuring your Spring application, check out the Spring docs for Enabling the MVC Java Config or the MVC XML Namespace.
In the case of JavaConfig, you'll substitute the
@EnableAutoConfiguration annotation for
@EnableWebMvc, and then add methods that programmatically define, for example, your template engine, such as Thymeleaf.
In the case of XML, you'll place your Spring configuration into one or more XML files placed in your classpath. The
resources directory is on the classpath, so these files can be placed here.
We've gotten a few things set up in our Spring application, but we've yet 0:00 to write even a single line of Java code. 0:03 Hang on though we're almost there. 0:06 It's time to write the configuration file for our application. 0:08 Throughout the years Spring has offered a variety of methods for 0:12 configuring a Spring application starting with XML. 0:15 Then to what's called Java config, and finally Spring boot. 0:19 Check the teacher's notes for links to the first two. 0:23 In this course, we'll be using Spring boot. 0:26 Of the three approaches, it's the simplest to implement. 0:30 Alright, let's configure our app. 0:34 To configure our app, first we need to create our base package, 0:37 under which we'll put all of our Java code. 0:40 We'll create this under source main Java and 0:42 call it com dot team treehouse dot giflib. 0:45 So I will right click the Java folder, choose new, 0:48 package and type com dot team treehouse dot giflib. 0:53 In this package we'll create a Java class called app config, 0:58 though it really could be named anything you like. 1:01 So, right click, new Java class called app config. 1:04 I'm going to delete this default comment block in here. 1:13 And because this going to be a runnable application, 1:16 I'm guessing you know exactly which method I'll need to add to this class. 1:19 Yep, it's a main method. 1:23 Public static void main. 1:25 String array args. 1:29 Wonderful. 1:32 In the main method, we're going to call a method from the spring boot library that 1:34 will run our application, creating a new application context. 1:37 The class is called spring application. 1:42 Now we get the suggestion here for 1:45 spring application because the Spring boot library is on our class path. 1:47 Gradle took care of that for us. 1:51 When you're using an IDE, and in Java in general, you can import any 1:53 class that's on your class path whether it came from the Java core library or 1:58 some sort of third party library like spring boot. 2:02 Spring application, I'll choose enter and 2:06 IntelliJ will automatically drop in that import statement for me. 2:08 And the method we want to call is run, and I'll choose our current class, 2:13 .class, passing it the args 2:18 parameter that was passed to the main method, and I'll save that class. 2:22 Finally, in order for 2:28 spring to be auto configured, we'll need to add an annotation to this class. 2:29 This annotation is called quite intuitively enable auto configuration. 2:34 Also coming from the spring framework. 2:40 And that's all we'll need to have a spring application that's deployable to 2:43 a web server. 2:46 Let's give it a whirl. 2:47 What you'll want to do at this point is, in the Gratde tool window, 2:49 expand tasks, and expand application, 2:55 right click on boot run, and choose run gitflib. 2:59 >> You should see some output that includes a fancy plain text Spring logo at 3:07 the top, and you should see in there that Tomcat, which is our web server, 3:12 was initialized, eventually was started, and finally that app config started. 3:16 Here you see the app config started in 2.067 seconds. 3:24 Now that our application is running in a live web server, 3:30 let's switch over to a browser and check it out. 3:33 In your favorite browser, I'm using Chrome here, you can find the local web server by 3:37 typing local host colon, 8080, which is the port number in the address bar. 3:41 I'll hit enter, and yay, there it is. 3:48 I know, you might be thinking, this dude's crazy. 3:53 He got an error message, and he's happy about it. 3:55 And you'd be right, about both parts. 3:57 But actually, this does indicate we were successful in running a web server and 4:00 deploying our application to the web server. 4:04 You see this message here that says this application has no explicit mapping for 4:07 slash error? 4:11 So you are seeing this as a fallback? 4:12 All that means is that we have no code in our application that could handle 4:15 the URL requested, which is essentially the home page. 4:18 So, it returns the error page which we also haven't defined. 4:22 Never fear though, our next step will fix that. 4:26 Congratulations on successfully deploying your first Java web app. 4:30 I know, the results may not be what you were hoping for from your first app, but 4:34 even without a complete application you've done quite a bit. 4:39 You've created a Spring application in IntelliJ, configured Gradle 4:43 which is one of the most widely used build and dependency management tools, and you 4:47 wrote a simple Spring configuration file that leverages the Spring boot library 4:52 to fire up your Spring web application and deploy it on a running web server. 4:56 That's pretty sweet. 5:01 Even cooler is that we're about to dive deep into coding giflib. 5:04 Why don't you go through a few questions and 5:08 challenges before we really tackle this application? 5:09 I'll see you right back here for some serious Spring coding. 5:13
You need to sign up for Treehouse in order to download course files.Sign up