In the previous step, we were able to create a REST service very easily. In the step, we will understand what
is auto configuration.
How did all the things that are needed for the REST
service to be up and running get configured automatically.
Let's look at it in this step.
We have the Spring Boot in 10 steps application where we have the at SpringBootApplication
annotation. The annotation at SpringBootApplication indicates that this is a Spring context file.
That's number one.
Number two, it enables something called auto configuration.
Number three, it enables something called component scan.
Component scan is one of the important features of Spring where it would start automatically scanning
these classes in this package.
And this sub package for any beans.
So we have added in an annotation at restController.
This is one of the annotations which is scanned for it.
So this would be registered as a component. So the bookController would be registered as a bean and it would be managed
by the Spring framework.
There are three things that are essentially done by at Spring Boot application.
It indicates that this is a Spring context.
The second thing is the fact that this enables auto configuration.
The third thing is that it enables automatic scan of this specific package.
Now if we want to look at auto configuration in dept. Spring application dot run method is used to run
a Spring context.
So I'm giving a Spring context as an input to it and it would be able to run that. The run method also
returns something. So it returns a application context.
So Ill actually get the application context back. So I'm getting the application context back.
This returns the application context. Im taking it and I would say application context..
I would want to loop around it.
So, for each, control one. Control space actually. I would want to loop around the application context.
dot get beans.
I want to get the names of all the beans which are configured and I would call this name and I would
want to log them. Ill for now do a sysout. Taking a shortcut here.
Ideally I should have been using a logger. But Im a lazy guy.
I would go ahead and print all the beans which are present in here.
Let's see what would happen.
Run us Java application.
OK! I'm getting an error saying, its failed to start because 8080 is already used.
Let's see.
It's already being used.
I'll kill that.
And let's start again.
Ok, cool!
There are a lot of things that are printed right now.
So there are a host of things that are getting configured for us automatically.
How are these being configured?
That's basically the feature called auto configuration. What Spring Boot does is as part of the Spring Boot
framework, there is something called Spring Boot autoconfigure.
So if I look at, one of the dependencies is Spring Boot auto configure. And this Spring Boot auto configure
has a lot of logic built into it. So you can see that there is a lot of classes that are written.
So if you look at the auto configuration Jar, it's a very very very big jar.
And if we go to the Web,
We will find our configurations for all the things that we are looking at right now.
I'm going in to the auto configure web servlet and you would see something called dispatcher servlet
auto configuration.
These auto configuration classes are what are creating these beans.
What would happen at startup is the Spring Boot framework would trigger the auto configuration jar.
And it would loop through classes which are on the classpath.
So it would see that, OK,
do I have a specific class on the classpath? It would see that I have a Spring web MVC framework on
the classpath.
What does this Spring Boot auto configure do?
It says, Oh! There is Spring web MVC on the classpath, then I would need to configure a dispatcher
servlet. It would say, Oh! there is Spring MVC on the classpath,
so I would need to configure a view resolver.
So all those kind of things happen and you'd be able to see all the beans which were created in here.
How does the configuration work?
Spring Boot basically looks at two things.
One is frameworks available on the classpath. There's a website called springboottutorial dot com, where we have
a lot of articles on Spring Boot.
I'm referring to one of the articles there.
So Spring Boot looks at A) the frameworks which are available on the classpath and also, B) It looks
at the existing beans which are configured for the application. And based on that, it provides the
configuration needed.
So for example, if you are using a web starter, the basic things that you would need would be a dispatcher
servlet, you would need a, probably, some internationalization, you need some messaging. All that kind of
stuff would get autoconfigured for you.
Now, we are looking at one of the auto configuration beans which is data source auto configuration.
If you see the code of data source auto configuration which is inside the Spring boot auto configure jar.
You would see that it's conditional on class data source dot class and an embedded database type dot class to
be present on the classpath.
But so what happens is only these classes are on the classpath,
then this specific bean would be created. The data source bean would be only created when these are on
the classpath.
But you can see the code in here. So data base auto configuration is conditional on a specific class being
available on the classpath.
But you can also see another example in here, So embedded data source database configuration.
This is only created when there is no datasource, when there is no bean created with a data source or
a XA datasource.
Spring Boot would then create an embedded database configuration for us. If you want to find out more about
what auto configuration is happening,
what we can do is we can turn on debug logging. What I can do is go to application dot properties.
That's one of the files which is present in here.
We'll talk about application properties a little later.
I'm turning on the logging level. All that you need to do is say logging dot level dot org dot Spring
Framework is equal to debug.
This would start the application in a debug mode.
Let me kill this up. Start the application again.
You can see a lot more logging being printed right now.
So a lot of things are being printed along with which
youd actually have a lot of logging about auto configuration which is printed in here.
So this will actually say all the stuff which was auto configured and which was not auto configured.
So its saying
dispatcher servlet auto configuration has matched because I found something of this kind in the classpath. So its saying,
I found a servlet called dispatcher servlet on the classpath. So I've configured dispatcher servlet automatically.
Its saying I've configured an error MVC auto configuration. I've already configured default error controller.
I've configured a cache, generic cache configuration. I've configured Jackson for doing binding and Ive auto
configured validation as well.
You can also see that there are things like view resolver and everything that were auto configured.
Not only that, the auto configuration report also shows what are the things that were not auto configured
So it would show why the rest of the auto configuration things did not match and why they were not
executed.
Why beans for them were not created.
Auto configuration is a very very important concept regarding Spring Boot. One of the most important
things to understand is you were still a beginner with Spring Boot and auto configuration is something
which will grow on you as you keep building more and more Spring Boot application.
If complete concept of auto configuration did not fit in your mind right
right now, that's not a problem at all.
You would definitely learn it by the time you end this course. Auto configuration is one of the fundamental
things
why Spring Boot is so famous and so easy to use. In this step,
We looked at the basics of auto configuration.
We looked at some of the beans which were automatically created and also we looked at where the code for
Spring Boot auto configuration is.
Until the next step, bye-bye.