Every Spring application maintained by the Spring IOC container is called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IOC container. These bean objects contain configuration metadata that we supply to the container.
The bean configuration metadata contains the set of properties.
Spring configuring metadata is defined in different ways.
By using XML file
By using Annotations.
By using Xml file
Here we are taken the number of properties in xml file.
[xml]
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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=" XXX " class=" XXX ">
<!-- collaborators and configuration for this bean -->
</bean>
<!-- A bean definition with lazy-init set on -->
<bean id=" XXX " class=" XXX " lazy-init="true">
<!-- collaborators and configuration for this bean -->
</bean>
<!-- A bean definition with initialization method -->
<bean id=" XXX " class=" XXX " init-method=" XXX ">
<!-- collaborators and configuration for this bean -->
</bean>
<!-- A bean definition with destruction method -->
<bean id=" XXX " class=" XXX " destroy-method=" XXX ">
<!-- collaborators and configuration for this bean -->
</bean>
<!-- more bean definitions go here -->
</beans>
[/xml]
Here we take an example program like Spring FirstApplication. In this example, we are discussed in before the lesson.
By using Annotation
We need to configure the configuration metadata by using annotations, Spring framework will provide some default Annotations.
1. @Bean
2. @Configuration
3. @Id
4. @Import
5. @Dependson.
We need to use these annotations in bean class, Spring container will automatically configure the main class to bean class.