Spring Java Beans in XML Configuration File
The XML configuration file called usually Beans.xml look in this way:
<?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”>
…
</beans>
Inside, we can write many things. The most important is our beans definitions. we can do that in many ways:
A simple bean definition looks:
<bean id=”…”>
Bean configuration
</bean>
A bean definition with lazy init set on looks:
<bean id=”…” lazy-init=”true”>
Bean configuration
</bean>
A bean definition with initialization method looks:
<bean id=”…” init-method=”…”>
Bean configuration
</bean>
A bean definition with destruction method looks:
<bean id=”…” destroy-method=”…”>
Bean configuration
</bean>
So we can see some properties that are used in Beans.xml files.
class – the class of your bean
name – your bean identifier that is unique
scope – the scope of your bean objects
constructor-arg –
properties –
autowiring mode –
lazy-initailization mode – your bean instance is created by IoC container when needed
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
Let’s look at the scope attribute.
singleton – a single instance per Spring IoC container; it’s default
prototype – any number of object instances
Scopes connected with Spring ApplicationContext:
request –
session –
global-session –
The example of the bean with singleton scope:
<?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” scope=”singleton”>
</bean>
</beans>
The example of the bean with prototype scope:
<?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” scope=”prototype”>
</bean>
</beans>
Life cycle of Spring bean
There are many things in the life cycle of Spring beans, but the most important are initialization and destruction. That’s why, we have init-method and destroy-method.
The initialization of our bean can be done in this way:
In Bean.xml, we have the following code:
<bean id=”mirelka” class=”com.linaittech.Mirelka” init-method=”initBean”/>
In the Mirelka.java file (our bean), we have the following code:
public class Mirelka {
public void initBean() {
our bean initialization code
}
}
The destruction of our bean can be done in this way:
In Bean.xml, we have the following code:
<bean id=”mirelka” class=”com.linaittech.Mirelka” destroy-method=”destroyBean”/>
In the Mirelka.java file (our bean), we have the following code:
public class Mirelka {
public void destroyBean() {
our bean destruction code
}
}
When we have many initialization and destruction methods, we don’t have to do it in each bean definition; we simply use this trick:
<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” default-init-method=”initBean” default-destroy-method=”destroyBean”>
<bean id=”…”>
Bean configuration
</bean>
</beans>
We see that <beans …> </beans> include default-init-method and default-destroy-method parameters.