(gentle music)
-: So till this point we were able to create a project,
the spring project, with the help of XML configuration.
So whatever beans we want Spring to manage,
we were doing that in the XML file.
And XML file was your spring.xml, right?
Of course this name can change.
The only thing you have to change then is this thing,
you know in the ClassPathXmlApplicationContext,
you have to mention that we are not working with spring.xml.
We are working with some other name, but that's fine.
We can go with a name.
But now, let's say we don't want to use xml.
A lot of people are not a big fan of XML,
so they prefer to go for Java configuration.
So yes, for the Spring you can do this configuration
using Java code, that's right.
So if you are happy with XML, continue with that.
If you are not happy with XML,
you can try out the Java based configuration.
Sometime you don't have a choice when you join a project
and if they're using XML, you have to use XML.
If they're using Java based configuration,
you have to use Java based.
We have one more way,
which is the annotation based configuration,
which we'll do later.
But let's see, how do you work
with the Java based configuration?
So what we do is the first thing if we change is
we don't want to use the XML approach
we want to use a Java approach.
So of course for that you need a a file, a Java class.
So what I'll do is in this package I will right click
and I will say, hey, I want to create a new class.
In fact, first let me create a new package called config.
So the config file will go in this.
And inside this config package,
I will rightly can say new Java class
and let me create a Java class as let's say AppConfig.
Again, name doesn't matter, you can give any name.
But let me go for AppConfig, user enter.
So you can see we got AppConfig file here
and now what we are going to do with this?
So this is going to replace your XML configuration, right?
So of course if you don't want to use this,
I'm not going to delete this from the project.
If you want to use it, I will just keep this file there.
I will just close this or maybe I will just keep it open
so that we can refer.
Go back to your App.java. This is where things will change.
So we don't want to go for this approach.
What I will do is I will just comment everything here.
In fact, first let me just uncomment this, yeah.
So let me comment everything
and let's do our coding on top here.
So if I go back up, imagine we don't have any of this code,
it's just that for your reference, I'm keeping it there.
But here, let me create object of application context
or the reference of application context,
I would say context is equal to new.
Now instead of using the XML approach,
we are going for a Java based approach.
For that we have to use a class
which is called AnnotationConfigApplicationContext,
let's go with this.
And in this particular constructor
you have to pass a name of your config file,
which is AppConfig.class.
The only thing is
you have to basically import the package for this
and package importing done.
So, in both the cases, even if you're using XML,
even if you're using a Java based configuration,
you are basically using the same container,
but the way you talk to a container changes.
So here we were using XML way of talking to the containers,
here we are using Java based configuration.
Now how do we do the configuration? That's a real question.
Okay, so what we will do is
let's create the object or let's use the object for a class.
Now let's not use Alien
because Alien is dependent upon computers,
so we have to do multiple things.
Let's work with desktop because if you look at desktop,
it's a independent class like laptop as well.
So, let me create the instance for desktop.
So I will say desktop, I will say des,
or maybe I'll say dt is equal to,
instead of saying new, I want to say context.getBean.
And I want to say I want the object of the desktop.class.
That's it.
So I want this to work.
And once you've got the object, I can also print something.
I can call the method, which is compile.
So it it should print something.
If you run this code, let's see what happens.
The moment you run this code, you got an error.
The error is no qualifying bean of type desktop available.
Of course, right?
Now we are not using a XML configuration.
We are using Java based configuration
and in this file we did nothing.
That's weird, right?
So if you want to make this work,
the first thing you will do is you will use annotation,
which is called configuration.
So if you want to configure your application
or if you want to configure
with the help of Java based configuration
we have to make your config class
with annotation called add configuration.
That's one.
What next?
We have one more tag
or one more annotation which we have to use, which is bean.
So in the XML, we were using bean tag.
Here we are going to use the bean annotation.
Okay, so what do you want? You want desktops.
I will just go back here, say desktop.
I mean I want the object for desktops.
I will say public, Desktop. And I will say desktop.
I want the object for desktop, right?
We have to import the package, let's do that, okay.
Now since this is a Java based configuration,
we have to manually create the object, okay?
So you will say return new desktop, that's it.
Now you'll be saying, hey, you know,
by using new keyword, we are cutting the object.
The thing about this, yes, we are writing this code here,
but then we are not injecting the object, right?
Spring is injecting it.
So who will call this particular method? Spring.
Who will manage the object? Spring.
We are not doing it,
we are just writing a code by saying new desktop.
But all this thing will be created, injected,
and managed by Spring framework.
That's something you have to remember.
Okay, so once we have done this,
the only thing you have to do here
is we have to add an annotation called bean.
So if you want your spring framework to create the object,
you have to use add bean.
Now this is similar to what we have done in the XML.
In the XML also we are reading bean tag, right?
The same thing we have done here.
We just shared using bean annotation.
And now let's see if this works.
Let's go back here, let's rerun and it works.
You can see it says compiling using desktop.
So what we have done is instead of using XML approach,
we are using the Java approach.
Even if we have this confusion still till now
that we are creating our own object here.
See we are just writing this code,
but this will be executed by Spring,
creating, injecting, managed by spring framework.
So we are not manually doing it.
Okay? So the approach which we have used here
is we are looking for the desktop class.
We are not specified the name of a bean,
we are looking for the type of the bean, which is desktop.
So it is looking for the desktop bean and it call it.
That's how we use the Java based configuration
with the help of this particular class here.
Now what if you want to use a bean name?
As of now you can see
nowhere we have mentioned the be name.
So what is a bean name and how do we change it?
Let's see that in the next video.