Spring Bean Definition – Spring Configuration Metadata
Interactive Online Courses: Elevate Skills & Succeed Enroll Now!
1. Objective
In our last tutorial, we studied Spring Boot CLI. Here, in this Spring Bean Tutorial, we will get to know about the Beans in Spring Framework and how to define Spring Bean. You will also see the properties of the Beans along with the working example using Eclipse IDE.
So, let’s start Spring Bean.
2. What is Spring Bean?
Spring Beans are the objects that form the backbone of the application. These are managed by the Spring IoC container. It does it by instantiating and assembling the bean object. These objects are created with the configuration metadata which is applied to the container like in the form of XML <bean/> which you have already seen.
The configuration metadata which contains the information about Spring Bean definitions needs the container to know the things like:
- Creating the Bean.
- Life-cycle details of Bean.
- Spring Bean Dependencies.
Do you know how to Install Spring Framework
All of the metadata configurations contains set of properties that make a Spring bean definition. These are as follows:
- Name: This attribute defines the Spring bean identifier differently. Like in XML based configuration you use id or name attributes to specify bean identifiers.
- Class: This attribute is the mandatory one. It specifies the bean class required to create the bean in Spring Framework.
- Scope: It specifies the scope of objects created from bean definition. You will see more about it in the later articles.
- Constructor arg: It is used for injecting dependencies in the Spring bean.
- Properties: This attribute is also used for injecting beans. You will see more about it in the later articles.
- Autowiring mode: There are several modes which are used for injecting beans. You will see more about it in the later articles.
- Lazy-initialization mode: This attribute tells the IoC container to create bean instance whenever it is requested unlike during the setup.
- Initialization method: It is a callback which is called after all the necessary properties are set by the container. Bean life cycle article will help you understand this clearly.
- Destruction method: It is a callback which is called when a container having Spring bean is destroyed.
3. Spring Configuration Metadata
There are three important methods which provide configuration metadata. These methods are:
- XML based configuration file.
- Annotation-based configuration.
- Java-based configuration.
You have already seen XML based configuration metadata but you will see an example with methods like lazy initialization, initialization, and destruction:
<?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation = "http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">   <!-- A simple bean definition -->   <bean id = "..." class = "...">      <!-- collaborators and configuration for this bean go here -->   </bean>   <!-- A bean definition with lazy init set on -->   <bean id = "..." class = "..." lazy-init = "true">      <!-- collaborators and configuration for this bean go here -->   </bean>   <!-- A bean definition with initialization method -->   <bean id = "..." class = "..." init-method = "...">      <!-- collaborators and configuration for this bean go here -->   </bean>   <!-- A bean definition with destruction method -->   <bean id = "..." class = "..." destroy-method = "...">      <!-- collaborators and configuration for this bean go here -->   </bean>   <!-- more bean definitions go here -->  </beans>2
Let’s Study about 5 Standard of Event Handlers
4. Example using Eclipse IDE
Now let’s define a Hello world example using the Eclipse IDE so as to get a better understanding of it with the steps as follows:
- Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
- Add the Spring Libraries that are required using the Add External JARs options.
- Create HelloWorld.java and MainApp.java under the above-made package.
- Finally, write code for all Java files and Bean config file and run the application as described.
The code for HelloWorld.java:
package com.example;
public class HelloWorld {   private String message;   public void setMessage(String message){      this.message = message;   }   public void getMessage(){      System.out.println("Your Message : " + message);   } }
The code for MainApp.java:
package com.example;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { Â Â public static void main(String[] args) { Â Â Â Â Â ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Â Â Â Â Â HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); Â Â Â Â Â obj.getMessage(); Â Â } }
Follow this link to know about Spring Custom EventÂ
The Beans.xml file is as defined:
<?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation = "http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">   <bean id = "helloWorld" class = "com.example.HelloWorld">     <property name = "message" value = "Hello World!"/>   </bean> </beans>
If you run the application and if everything is correct you will achieve the following output:
Your Message: Hello World!
The annotation based configuration is described in the coming article (annotation based) separately.
So, this was all about Spring Bean Tutorial. Hope you like our explanation.
5. Conclusion
Hence, in this article, we have learned about the Spring Bean definition and the configuration metadata. We also saw how they are managed by the Spring IoC container. A working example was also defined so as to get detail analysis of the topic. If you have any query, feel free to ask in the comment section.
Related Topic-Â Spring Framework Architecture
For reference
Did we exceed your expectations?
If Yes, share your valuable feedback on Google