Site icon DataFlair

Spring Bean Definition – Spring Configuration Metadata

FREE Online Courses: Enroll Now, Thank us Later!

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.

Spring Bean Definition – Spring Configuration Metadata

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:

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:

3. Spring Configuration Metadata

There are three important methods which provide configuration metadata. These methods are:

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:

  1. Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
  2. Add the Spring Libraries that are required using the Add External JARs options.
  3. Create HelloWorld.java and MainApp.java under the above-made package.
  4. 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);
   }
}

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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

Exit mobile version