Daily Archives: November 30, 2013

ApplicationContext or AbstractApplicationContext in Spring Framework

ApplicationContext or AbstractApplicationContext in Spring Framework

With using the ApplicationContext interface.

package com.linaittech1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
       
        ApplicationContext context = new FileSystemXmlApplicationContext (“file:/home/poganin/worksSpringer/Spring2/src/Beans.xml”);
        Mirek obj = (Mirek) context.getBean(“mirek”);
        obj.getMessage();
       
        ApplicationContext context1 = new FileSystemXmlApplicationContext (“file:/home/poganin/worksSpringer/Spring2/src/Beans.xml”);
        Lak obj1 = (Lak) context1.getBean(“lak”);
        obj1.getNum1();
        obj1.getNum2();
        obj1.getSum();
       
    }

}

With using the AbstractApplicationContext interface.

package com.linaittech1;

import org.springframework.context.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
        
        AbstractApplicationContext context = new FileSystemXmlApplicationContext (“file:/home/poganin/worksSpringer/Spring2/src/Beans.xml”);
        Mirek obj = (Mirek) context.getBean(“mirek”);
        obj.getMessage();
        context.registerShutdownHook();
        
        AbstractApplicationContext context1 = new FileSystemXmlApplicationContext (“file:/home/poganin/worksSpringer/Spring2/src/Beans.xml”);
        Lak obj1 = (Lak) context1.getBean(“lak”);
        obj1.getNum1();
        obj1.getNum2();
        obj1.getSum();
        context1.registerShutdownHook();
        
    }

}

You probably see the difference. With AbstractApplicationContext, we have a great possibility to shutdown our bean. Sometimes, it’s simply good to use the second method.

Spring Java Beans in XML Configuration File

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&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

</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&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<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&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<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&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&#8221; 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.