Spring provides two types of configuration, XML based which came in market first and then Java based. Since we already know what are the basics behind Spring configuration, anything and everything is bean. So how does spring supports that.
Let’s see some code first.
This can be related to a XML file, not exactly though but it defines a class annotation with @Configuration will mainly be used to configured spring bean. Even a configuration class will have a singleton instance in spring container.
@Bean mostly used over getter methods which returns a object of a class. This you can relate to
Scope attribute helps us to define the scope of the bean which have learnt in previous blog singleton, prototype, request, session, global and global session.
Pros of Java Based Configuration.
- Java is type safe, you will get error if try to do something wrong.
- Refractoring code very easy.
Pros of XML Based Configuration.
- Centralized configuration, you don’t need to search your code for a bean you can find everything in XML.
- You can Hot patch XML file easily, as these wouldn’t get’s converted to class file.
- You can define explicit name of bean and wire them.