Spring – 13 – Bean Lifecycle – 01 – Spring Awareness

Merhaba Arkadaslar.
Bu bolumde Bean Lifecyle’ina goz atacagiz ve Spring Awareness konusunu inceleyecegiz.

spring bean lifecycle

  • Bean tanimlamasi/definition icin XML dosyalari ve Java dosyalari taranir.
  • Bean objeleri olusturulur.
  • Bean property’leri icin Dependency Injection gerceklestirilir.
  • Daha sonrasinda Spring Awareness (farkindalik , haberdar olma) adimina gecilir. Bu bolumde iste bu kisimdan bahsedecegiz. Sonraki yazilarda diger blocklari sirasiyla inceleyecegiz.

Spring Awareness

Awareness farkindalik , haberdar olma anlamina gelmektedir. Yukarida Lifecycle diagraminda Spring Awareness bolumunde 3 tane arabirim yer almaktadir;

  • BeanNameAware
  • BeanClassLoaderAware
  • ApplicationContextAware

BeanNameAware arabiriminde sadece setBeanName metodu tanimlidir.

void setBeanName(String name);

setBeanName metodu , bean olustuktan hemen sonra calisir.

SpringAwarenessModel.java

import org.springframework.beans.factory.BeanNameAware;

public class SpringAwarenessModel implements BeanNameAware {

	private String beanName;

	@Override
	public void setBeanName(String name) {
                System.out.println("Setting name...");
		this.beanName = name;
	}

	public void log() {
		System.out.println("Bean[" + beanName + "] information log...");
	}

}

16.springAwareness.xml

<bean id="springAwarenessWriter" class="_16.springAwareness.model.SpringAwarenessWriter" />

SpringAwarenessTest.java

package _16.beanNameAware.test;
//imports
public class SpringAwarenessTest {
	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("16.beanNameAware.xml");

		SpringAwarenessWriter springAwarenessWriter = ctx.getBean("springAwarenessWriter", SpringAwarenessWriter.class);
		springAwarenessWriter.log();

		((ClassPathXmlApplicationContext) ctx).close();

	}
}

BeanNameWriter icin bean objesi olustuktan sonra setBeanName metodu calisacaktir.
Ornegimizi calistirdigimizda ;

Setting name...
Bean[springAwarenessWriter] information log...

BeanClassLoaderAware arabiriminde setBeanClassLoader metodu yer almaktadir.

void setBeanClassLoader(ClassLoader classLoader);

SpringAwarenessWriter.java

public class SpringAwarenessWriter implements BeanNameAware, BeanClassLoaderAware {
...
	// BeanClassLoaderAware
	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
	    System.out.println("class loader:" + classLoader.getClass());

	}
...
}

Class Loader hakkinda daha fazla bilgi icin ; How ClassLoader Works in Java

Son olarak ApplicationContextAware arabirimini uygulayalim. ApplicationContextAware arabiriminde setApplicationContext metodu yer almaktadir.

void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

SpringAwarenessWriter.java

public class SpringAwarenessWriter implements BeanNameAware, BeanClassLoaderAware, ApplicationContextAware {
...
	// ApplicationContextAware
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	    System.out.println("setApplicationContext");
	    System.out.println(applicationContext.getDisplayName());
	}
...
}

Tum arabirimleri uygulayip ornegimizi calistirdigimizda ;

Setting name...
class loader:class sun.misc.Launcher$AppClassLoader
setApplicationContext
org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d
Bean[springAwarenessWriter] information log...

Dikkat edecek olursak oncelikle BeanNameAware setBeanName metodu sonrasinda BeanClassLoaderAware setBeanClassLoader son olarak da ApplicationContextAware setApplicationContext metodu caliscaktir.

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 *