This video introduces the basic concept of the application context and how it is leveraged in a Spring application.
- [Instructor] The ApplicationContext is the heart of an application leveraging the Spring Framework. And as such, understanding what it is and how it works is central to learning the Spring Framework as a whole. We will therefore start our journey of the Spring Framework in depth with understanding the ApplicationContext. If the ApplicationContext is the heart, the BeanFactory is the ventricles of that heart. While we seldom delve into the BeanFactory itself, it is important to know it exists once we dig into the lifecycle of a SpringApplication.
For now, however, just know that the ApplicationContext is a read-only wrapper of that BeanFactory, and all of your runtime interactions with the BeanFactory or any beans contained in that factory is through this ApplicationContext. The ApplicationContext provides the metadata for all beans created, and also provides a mechanism for creating beans in the correct order. The ApplicationContext is the Inversion of Control Container and all of your Dependency Injections occur here.
In fact, much of Spring is configuring the context to control the Dependency Injections. The ApplicationContext manages all of our singleton beans in the application itself. It also serves all prototype or session scoped beans. We will have a little bit more on those scopes later on. As we go through this course, we will initially interact directly with an implementation of the ApplicationContext, but most normal use cases have no need to directly interact with the context itself.
That being said, any class can be made aware of the ApplicationContext through appropriate Spring interfaces, but that is beyond the scope of this topic for now. So, let's take a look at a simple example of getting a Spring-managed bean from the ApplicationContext. I don't want to focus too much on what is going on because we will dig into all of this much deeper, but I want you to start with a simple Maven structure Java project.
If you have access to the Exercise Files, this is created for you. So, one of our first tasks here is to bring the Spring dependencies into our project, so we're going to open up the pom.xml file, and we're actually going to start with a dependencyManagement section. Now, in this dependencyManagement section, we're going to add the platform-bom for Spring. Now, this is from the io.spring.platform group and the version we are using is Brussels-SR3.
Now, there's a couple other things that we need to add. We need to specify that this bom is actually a pom file, and we need to set its scope to be import only. Now we can actually go in and specify our dependencies, so we'll create a dependency section and add our first dependency. And the first dependency that we are going to add is spring-core. Now, spring-core comes from the org.springframework group.
One of the first things you will see here is that I'm not adding a version. That version is actually coming from the platform-bom. With Spring Platform, all of the versions of Spring have been guaranteed to work together in a specific Platform version. In addition to the Spring dependency, we also will get third-party dependencies, so let's go ahead and bring in a couple more dependencies here from the Spring libraries. The first one we're going to bring in is spring-beans, and again, spring-beans comes from org.springframework, and now we will bring in the spring-context project, again from org.springframework.
And now we have one more dependency to add, and that is actually a commons-logging dependency. And you'll see here, we haven't actually put a version in again. That is one of the dependencies added from the platform-bom to ensure compatibility. So, at this point, we're actually done and if we take a look at our External Libraries, you will see that we've brought in all of our dependencies and a couple transitive dependencies. Let's go ahead and navigate now to src main java, and in the com.lynda folder, I want you to create a new class and that new class is going to be HelloConfig.
Now, I don't want you to focus too much on what's going on because we are going to explain all of this later, but I do want to simply show you how to get a bean out of a context. So, we're going to annotate our class with @Configuration, and we are going to create a bean and that bean is going to be of type String, and we are going to call it greeting and we will simply return the String "World." Now we are going to make a main method, so public static void main and to that we need to pass a String array called args.
And now here's where we're actually going to get into the ApplicationContext itself. So, we will get a handle to the ApplicationContext and we'll call it context, and we will create a new implementation of the AnnotationConfigApplicationContext. Now, this class expects a class to be passed in, in its constructor, that has been annotated with @Configuration, so we will pass in an instance of the existing class that we are working in, and now we will create an instance of a variable called text and we will actually get that text from the context itself by calling context.getBean and passing it in a bean name that is equal to our method.
Now, at this point, if I do a System.out.println, and I type in "Hello," and as I'm sure you can guess, we will set it to text. So, what will happen when we actually run this is that we are going to print out Hello, World where the String "World" come from our bean documented on line 11. So, let's go ahead and run this class, we'll head into our compilation, and now you'll see, we actually have the console running.
And on the fifth line of the console here, we actually have the String "Hello, World" as we would expect. So, one final point to note is that we can actually have more than one ApplicationContext running at a given time. In fact, in a web world, we always have more than one. Now, the parent context you seldom will interact with, but it has a handle to the context that you do interact with. And as with all good software, you can always interact with your children, but they cannot interact with you.
So, now that we've experienced the basics of the context, let's increase our knowledge and dive into configuring that context.
- Introduction to Spring
- Configuring the ApplicationContext
- Using the Spring expression language
- Configuring proxies
- Autowiring beans
- Using lifecycle methods
- Configuring beans with XML
- Understanding the initialization phases of the bean lifecycle
- Aspect-oriented programming and Spring