From the course: Spring: Framework in Depth

Configuration of Spring with Java

From the course: Spring: Framework in Depth

Start my 1-month free trial

Configuration of Spring with Java

- [Instructor] We're going to start our journey of configuring Spring with Java config. While it is not the first way to configure, it is the most current typed configuration method in use today. XML is still valid, but not often used, and has been removed from most of the examples on Spring.io. Generally, it's just not favored anymore. Sometimes, no matter what you do, you need to type the config for Spring applications, and Java config should be your first choice, because of the benefits. First off, it provides native language syntax. There is no other language, schema, or structure to consider, you just use Java. It also provides compile time checking of object creation and syntax because it is a native language. This compile time checking is very welcome, especially if you have used XML syntax where you would have to run the application to test the actual configuration. And because it's Java, it integrates better in most cases with your IDE. This is less critical with more modern plug ins for your IDE of choice, but it does offer nice syntax highlighting and auto completion in most IDEs. So let's take a look at this in real life, using the sample exercise files that I've provided for you. So I want you to open up your exercise files like I have done in my IDE. So I'm currently at this version, and you'll see I've got an application, and this application has several service classes that run with it. I'm going to go ahead and run my application. Now while this is running and spinning through, I want to talk a little bit about this exercise. So really this is an overly complex implementation of a Hello World program. But it's just so we can show you the details of Spring. This is by no means a real application, nor would I ever write one this simple to be this complex. So while this main method is running, and I did that through my IDE using a run configuration, there's various ways to run a Java main method, and I'll let you run through those opportunities, but my IDE has a nice plug in that allows me to just run the main method, and configure it as needed. All right, so let's make this thing Spring enabled, so we can start configuring it. The first thing that we want to do is open up the pom file, and you'll see we have a very simple pom file, and we're going to add some stuff to this. Let me close this terminal window here. We're going to add a dependency section, and to that we're going to add a couple dependencies. Now the first dependency that we are going to add is Spring core, and Spring core comes from org.Spring framework. Now we need to have a version number, and to do that I'm going to set a property up here to be Spring.version. And at this point in time I'm going to use 5.2.3.release. Now with Spring there are several ways to handle dependencies. For the purposes of this we're just going to use standard releases. Let's go ahead and set that version, as a Spring.version variable, from the property, and now we're going to add another dependency, and this one is Spring dash context. And this will allow us to actually configure what we're trying to do here. Once again, this comes from org.Spring framework, and we will once again share a version of dollar sign Spring.version. Now you'll notice I am using a property here, I think it's very important with Spring to do that, because you really want your versions to be connected in some way, shape, or form. If you're using Spring Boot this has all been done for you, through the use of a parent pom. So you don't have to do that in this case. So now let's jump over here to source main Java, in our package com Frank Moley L-I-L-F-I-D, and we want to create a new package, and we're going to call this package config. And in our config package we'll create a new class file, and we will simply call it application config. Okay, so now to our application config file we're going to put a few annotations in here, and don't worry too much about them, we're going to talk a lot about them later on. But we're going to set a value of hello, to a private string that we will call greeting. Go ahead and import that annotation, and now we're going to set up a bean. And the bean is really what we're using to define a Spring application. So we will define a time service bean, that we will call time service, and we're simply going to return a new time service, and for right now we'll just set it to true for the age 24 parameter on time service. Now obviously, I know what these are by heart, because I've written this code, but take a look at these classes that I'm defining so you can see how the constructors are set up. Now we're going to create an output service bean, called output service, and we will return a new output service that takes a greeting service and a time service. And we'll get those populated here in a moment, and now we're going to create one more bean, and this one is called greeting service, and our greeting service takes a greeting, and the greeting we'll provide right now is hello. Now we're going to annotate this class with at configuration, and we need to auto wire in a couple private beans here, just so that this will work for now. This is not how this is going to end up looking at the end, but for right now this serves its purpose. What I really want to show you is how you're going to create the application context. So let's jump into our application class, which is at the root package structure, and you'll see I have a very simple implementation here, we're going to basically nuke most of this, and the way that we are going to do this is we are going to create an instance of application context, and we are going to use the annotation config application context, and we will pass it our application config.class. Go ahead and import those values, and now we are going to create an output service bean, called output service, and we will get that from our context. So we're going to be called context get bean, and we need to get an instance of output service class from the context. Now you'll see we haven't actually gone and created any beans that we're using, we're pulling them out of the application context. So now if we run our application, Spring should use that Java config, wire up the application, and we should get the exact same results in our console that we had before. Which is nothing more than Hello World printed out five times, once every five seconds. And that's all there is to using the very basics of Java config to get an application context up and running.

Contents