Java @ Desk: Spring
Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Wednesday, December 25, 2013

Spring InitializingBean interface init bean annotation

9:21 AM 0
Spring InitializingBean interface init bean annotation

A bean configured in spring configuration file gets initialized when the file gets loaded in the spring container. There may arise a case where something needs to be initialized on bean initialization or needs to be destroyed before the bean nullifies.

Consider an example, the Properties file need to be loaded as soon as bean gets initialized. What happens to this property file when the bean gets destroyed. The best practice is to clear this property file. Also if the property file gets loaded on first client request, it would take additional time for the first request.

In order to resolve both, the property file gets loaded on bean initialization itself plus gets destroyed once the bean is destroyed using the following annotations :
1) Override afterPropertiesSet - Belongs to org.springframework.beans.factory.InitializingBean package. Override the function to provide a callback function (afterPropertiesSet()) which the ApplicationContext will invoke when the bean is constructed
2) @PreDestroy - Belongs to javax.annotation.PreDestroy package

In case of annotation, Spring container will not be aware of @PreDestroy annotation. To enable it, either of the following things need to be taken care of in apring configuration file:
 1) Specify the <context:annotation-config />  
 2) Specify the <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  

Below is the sample implementation for this:
1) Spring Configuration file
<?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-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <context:annotation-config />

 <bean id="springInitDestroy" class="com.spring.SpringInitDestroy">
 </bean>

</beans>
2) SpringInitDestroy Bean
package com.spring;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.InitializingBean;

public class SpringInitDestroy implements InitializingBean {
 @PreDestroy
 public void cleanUp() throws Exception {
  System.out.println("Init method called on bean destroy");
 }

 @Override
 public void afterPropertiesSet() throws Exception {
  System.out
    .println("Init method called on bean initialization - afterPropertiesSet");
 }
}
3) SprintInitDestroyTest Client File
package com.spring;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SprintInitDestroyTest {
 public static void main(String[] args) {
  ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "com//spring//appContext.xml" });

  SpringInitDestroy initDestroy = (SpringInitDestroy) context
    .getBean("springInitDestroy");

  System.out.println("Bean Created : " + initDestroy);

  context.close(); // @PreDestory method gets called here
 }
}

Output :
Init method called on bean initialization - afterPropertiesSet
Bean Created : com.spring.SpringInitDestroy@197bb7
Init method called on bean destroy

Spring @PostConstruct & @PreDestroy init bean method annotation

8:57 AM 0
Spring @PostConstruct & @PreDestroy init bean method annotation

A bean configured in spring configuration file gets initialized when the file gets loaded in the spring container. There may arise a case where something needs to be initialized on bean initialization or needs to be destroyed before the bean nullifies.

Consider an example, the Properties file need to be loaded as soon as bean gets initialized. What happens to this property file when the bean gets destroyed. The best practice is to clear this property file. Also if the property file gets loaded on first client request, it would take additional time for the first request.

In order to resolve both, the property file gets loaded on bean initialization itself plus gets destroyed once the bean is destroyed using the following annotations :
1) @PostConstruct - Belongs to javax.annotation.PostConstruct package
2) @PreDestroy - Belongs to javax.annotation.PreDestroy package

In case of annotation, Spring container will not be aware of @PostConstruct and @PreDestroy annotation. To enable it, either of the following things need to be taken care of in apring configuration file:
 1) Specify the <context:annotation-config />  
 2) Specify the <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  

Below is the sample implementation for this:
1) Spring Configuration file
<?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-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 <context:annotation-config />

 <bean id="springInitDestroy" class="com.spring.SpringInitDestroy">
 </bean>

</beans>
2) SpringInitDestroy Bean
package com.spring;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class SpringInitDestroy {

 @PostConstruct
 public void initIt() throws Exception {
  System.out.println("Init method called on bean initialization");
 }

 @PreDestroy
 public void cleanUp() throws Exception {
  System.out.println("Init method called on bean destroy");
 }
}
3) SprintInitDestroyTest Client File
package com.spring;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SprintInitDestroyTest {
 public static void main(String[] args) {
  ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
    new String[] { "com//spring//appContext.xml" });

  SpringInitDestroy initDestroy = (SpringInitDestroy) context
    .getBean("springInitDestroy");

  System.out.println("Bean Created : " + initDestroy);

  context.close(); // @PreDestory method gets called here
 }
}

Output :
Init method called on bean initialization
Bean Created : com.spring.SpringInitDestroy@1a897a9
Init method called on bean destroy

Sunday, October 13, 2013

Hibernate annotation example with Spring

12:10 AM 0
Hibernate annotation example with Spring

The web application to be created using Struts Spring and Hibernate can be configured in 6 easy steps. This configuration is an example to configure hibernate using .hbm files using the HibernateDaoSupport as shown below:
1) Configure the datasource - This is configured in the spring application context xml file. Configure the bean for datasource using class org.springframework.jdbc.datasource.DriverManagerDataSource. The class can be found in jar spring-jdbc-2.0.7.jar
Load Database Configuration from Properties file

Saturday, October 12, 2013

Integrate Struts Spring Hibernate web application

11:29 PM 0
Integrate Struts Spring Hibernate web application

The web application to be created using Struts Spring and Hibernate can be configured in 6 easy steps. This configuration is an example to configure hibernate using .hbm files using the HibernateDaoSupport as shown below:
1) Load the Spring application context xml file in struts-config - This can be achieved using the Spring pugin for struts org.springframework.web.struts.ContextLoaderPlugIn class. The jar in which you can find this class is org.springframework.web.struts-3.0.1.RELEASE.jar

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">  
 <set-property property="contextConfigLocation"  
 value="/WEB-INF/springconfig/applicationContext.xml"/>  
 </plug-in>

Tuesday, July 2, 2013

Load properties file in spring application context

4:00 AM 0
There are two ways to load a property file in spring configuration xml file.

1) Using org.springframework.beans.factory.config.PropertyPlaceholderConfigurer class
2) Using tag <context:property-placeholder/>

1) Using org.springframework.beans.factory.config.PropertyPlaceholderConfigurer class

spring-config.xml

Friday, June 28, 2013

How to load spring application context xml from classpath in java

4:27 AM 0
This article will explain you how to load a spring configuration/application context xml file in standalone java application.
ApplicationContext is an interface that is being implemented in Spring to load a spring configuration in a java class. The ClassPathXmlApplicationContext class implements this interface which takes a path to the spring configuration application context xml file located in a classpath.
Below is the sample implementation to load a xml file and load a bean from that xml file in java.

Friday, June 21, 2013

Bean scope in spring

11:43 PM 1
When we declare a bean in spring, there is a feature to define the scope of that bean in the configuration file. In spring, there are 5 different types of scopes as shown below:

Scope Explanation
singleton Return a single bean instance per Spring IoC container
prototype Return a new bean instance each time when requested
request Return a single bean instance per HTTP request
session Return a single bean instance per HTTP session
globalsession Return a single bean instance per global HTTP session