Spring – 14 – Bean Lifecycle – 02

Merhaba Arkadaslar
Bu bolumde Bean Lifecycle konusuna devam edecegiz. Onceki bolumde Spring Awareness adimlarini incelemistik bu bolumde Bean Creation Lifecyle ve Bean Construction Lifecycle konusunu inceleyecegiz.

Bean Creation Lifecyle

Bean Creation Lifecycle adimlarina bakacak olursak ;

  • @PostConstruct annotation varsa ilgili metotlar calisacaktir.
  • InitializingBean interface’si uygulanmissa afterPropertiesSet metodu calisacaktir.
  • XML konfigurasyonunda <bean> taniminda init-method tanimi yapilmissa bu metot calisacaktir.

bean creation lifecycle

@PostConstruct annotation’i metot uzerine yazabiliriz. InitializingBean arabirimini uyguladigimizda afterPropertiesSet metodunu override etmemiz gereklidir.

BeanCreationLifecycleWriter.java

package _17.beanCreationLifecycle.model;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.InitializingBean;

public class BeanCreationLifecycleWriter implements InitializingBean {

	private String property1;

	public String getProperty1() {
		return property1;
	}
	
	public void setProperty1(String property1) {
		this.property1 = property1;
	}
	
	@PostConstruct
	public void postConstructTestMethod() {
		System.out.println("PostConstruct...");
	}
	
	@PostConstruct
	public void postConstructTestMethod2() {
		System.out.println("PostConstruct 2...");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet... ");
	}
	

	public void initTestMethod() {
		System.out.println("initializing...");
	}

	@Override
	public String toString() {
		return "BeanCreationLifecycleWriter [property1=" + property1 + "]";
	}

}

@PostConstruct annotation’i kullanabilmek icin <context:annotation-config> tag’ini eklmememiz gereklidir!ya da <context:component-scan> tagini kullanabiliriz.
Dikkat edecek olursak bean taniminda init-method attribute’unu kullandik.

17.beanCreation.lifecycle.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config />
	<!--@PostConstruct annotation'i kullanabilmek icin context:annotation-config tag'ini eklmememiz gereklidir!
	ya da context:component-scan kullanabiliriz.
	-->

	<!--init-method attribute'u kullaniyoruz!-->
	<bean id="lifecycle"
		class="_17.beanCreationLifecycle.model.BeanCreationLifecycleWriter"
		init-method="initTestMethod">
		<property name="property1" value="myProperty" />
	</bean>

</beans>

BeanCreationLifecycleTest.java

package _17.beanCreationLifecycle.test;

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

import _17.beanCreationLifecycle.model.BeanCreationLifecycleWriter;

public class BeanCreationLifecycleTest {
	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("17.beanCreation.lifecycle.xml");

		BeanCreationLifecycleWriter beanCreationLifecyle = ctx.getBean("lifecycle",
				BeanCreationLifecycleWriter.class);
		
		System.out.println(beanCreationLifecyle);
		
		((ClassPathXmlApplicationContext) ctx).close();

	}
}

Ornegimizi calistirdigimizda ;
Burada @PostConstruct annotation’li metotlarin calisma sirasi unpredictable/ongorulemezdir.

PostConstruct...
PostConstruct 2...
afterPropertiesSet... 
initializing...
BeanCreationLifecycleWriter [property1=myProperty]

Bean Destruction Lifecycle

Bean Destruction Lifecycle adimlarini inceleyecek olursak ;

  • @PreDestroy annotation varsa ilgili metotlar calisacaktir.
  • DisposableBean arabirimi uygulanmissa destroy metodu calisacaktir.
  • XML konfigurasyonunda <bean> taniminda destroy-method tanimi yapilmissa bu metot calisacaktir.

bean destruction lifecycle

@PreDestroy annotation’ini birden fazla metot icin kullanabiliriz. DisposableBean arabiriminde destroy metodu yer almaktadir.

package _18.beanDestructionLifecycle.model;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;

public class BeanDestructionLifecycleWriter implements DisposableBean {

	private String property1;

	public String getProperty1() {
		return property1;
	}

	public void setProperty1(String property1) {
		this.property1 = property1;
	}

	@PreDestroy
	public void preDestroyTestMethod() {
		System.out.println("PreDestroy...");
	}

	@PreDestroy
	public void preDestroyTestMethod2() {
		System.out.println("PreDestroy 2...");
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("DisposableBean - destroy method");
	}

	public void testDestroyMethod() {
		System.out.println("Destroying...");
	}
        
        @Override
        public String toString() {
               return "BeanDestructionLifecycleWriter [property1=" + property1 + "]";
        }
 

}

18.beanDestruction.lifecycle.xml

...
	<context:annotation-config />

	<bean id="lifecycle"
		class="_18.beanDestructionLifecycle.model.BeanDestructionLifecycleWriter"
		destroy-method="testDestroyMethod">
		<property name="property1" value="myProperty" />
	</bean>
...

BeanDestructionLifecycleTest.java

package _18.beanDestructionLifecycle.test;

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

import _18.beanDestructionLifecycle.model.BeanDestructionLifecycleWriter;

public class BeanDestructionLifecycleTest {
	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("18.beanDestruction.lifecycle.xml");

		BeanDestructionLifecycleWriter beanDestruction = ctx.getBean("lifecycle",
				BeanDestructionLifecycleWriter.class);

		System.out.println(beanDestruction);

		((ClassPathXmlApplicationContext) ctx).close();

	}
}

Ornegimizi calistirdigimizda ;

BeanDestructionLifecycleWriter [property1=myProperty]
Oct 16, 2015 11:16:18 PM org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Fri Oct 16 23:16:18 EEST 2015]; root of context hierarchy
PreDestroy...
PreDestroy 2...
DisposableBean - destroy method
Destroying...

Github kaynak dosyalar/ source folder
leventerguder/injavawetrust-spring-tutorial

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *