Spring Bean Definition:-
The objects that form the backbone of your application and that are managed by
the Spring IoC container are called beans. A bean is an object that is
instantiated, assembled, and otherwise managed by a Spring IoC container. These
beans are created with the configuration metadata that you supply to the
container, The bean definition contains the information called configuration
metadata which is needed for the container to know the followings:
1 How to create a bean
2 Bean's lifecycle details
3 Bean's dependencies
All the above configuration metadata translates into a set of the following properties that make up each bean definition.
Properties |
Description |
class |
This attribute is mandatory and specify the bean class to be used to create the bean. |
name |
This attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). |
scope |
This attribute specifies the scope of the objects created from a particular bean definition. |
constructor-arg |
This is used to inject the dependencies |
properties |
This is used to inject the dependencies. |
autowiring mode |
This is used to inject the dependencie. |
lazy-initialization mode |
A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup. |
initialization method |
A callback to be called just after all necessary properties on the bean have been set by the container. |
destruction method |
A callback to be used when the container containing the bean is destroyed |
Spring Configuration Metadata:-
Spring IoC container is totally decoupled from the format in which this
configuration metadata is actually written. There are following three important
methods to provide configuration metadata to the Spring Container:
1 XML based configuration file
2 Annotation-based configuration
3 Java-based configuration
1 XML based configuration file:-The XML Schema-based configuration
introduced in Spring 2.0 and enhanced and extended in Spring 2.5 and 3.0.
The central motivation for moving to XML Schema based configuration files was to
make Spring XML configuration easier. The 'classic' <bean/>-based approach is
good, but its generic-nature comes with a price in terms of configuration
overhead.
<?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="..." class="..."></bean>
<bean id="..." class="..." lazy-init="true"></bean>
<bean id="..." class="..." init-method="..."></bean>
<bean id="..." class="..." destroy-method="..."></bean>
</beans>
2 Annotation-based configuration:- Spring 2.5 introduced support for annotation-based configuration metadata.Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- bean definitions here -->
</beans>
Some important annotations are as follows:-
1 @Required The @Required annotation applies to bean
property setter methods.
2 @Autowired The @Autowired annotation can apply to bean property setter
methods, non-setter methods, constructor and properties.
3 @Qualifier The @Qualifier annotation along with @Autowired can be used
to remove the confusion by specifiying which exact bean will be wired.
4 JSR-250 Annotations Spring supports JSR-250 based annotations which
include @Resource, @PostConstruct and @PreDestroy annotations.
3 Java-based configuration:- Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the @Configuration, @Bean, @Import and @DependsOn annotations.
package com.r4r.snippets.enterprise;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.r4r.snippets.enterprise.services.HelloWorldService;
@Configuration
public class TestConfig {
@Bean(name="helloWorldBean")
public HelloWorldService helloWorldService() {
return new HelloWorldService();
}}
The @Bean annotation over the helloWorld() method indicates that this method produces a bean to be managed by the Spring container.