Spring Ders06 - Bean Scopes


   Bir bean, getBean ile .xml dosyasından birkaç kez çağrıldığında daha önce çağrılan beanin mi kullanılacağı yoksa yeni bir örnek bean mi oluşturulacağı bean scopela belirlenir.

5 tane bean scope vardır.
  • singleton
  • prototype
  • request
  • session
  • globalSession
singleton :
  • Her bean çağrılışında aynı bean örneği kullanılır. Bean ancak container yeniden örneklenirse yeni bir bean üretilir. Scope seçilmezse default olarak singleton scope kullanılır.
prototype :
  • Her bean çağrılışında yeni bean örneği kullanılır.
request : 
  • Web uygulamalarında geçerlidir. Bean HTTP isteği üzerinde tanımlanmıştır. Her HTTP isteğinde yeni bir bean örneklenir.
session :
  • Web uygulamalarında geçerlidir. Bean HTTP oturumu (session) üzerinde tanımlanmıştır. Http session boyunca aynı bean kullanılır.
globalSession :
  • Portal uygulamalarda geçerlidir.

Şimdi bir örnek yapalım.

Main.java
package test;


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

public class Main {

 public static void main(String[] args) {
  
  
  ApplicationContext context= 
    new ClassPathXmlApplicationContext("applicationContext.xml");
  
  Personel p1=(Personel) context.getBean("pers1");
  Personel p2=(Personel) context.getBean("pers1");
  
  System.out.println("p1 ve p2");
  if(p1==p2){
   System.out.println("aynı bean örnegi\n\n");
  }
  else{
   System.out.println("farklı bean örnegi\n\n");
  }
  
  
  Personel p3=(Personel) context.getBean("pers2");
  Personel p4=(Personel) context.getBean("pers2");
  
  System.out.println("p3 ve p4");
  if(p3==p4){
   System.out.println("aynı bean örnegi\n\n");
  }
  else{
   System.out.println("farklı bean örnegi\n\n");
  }
 }
}

Personel.java
package test;


public class Personel {

 private String ad;
 private int yas;

 public Personel() {
 }

 public Personel(String ad, int yas) {
  this.ad = ad;
  this.yas = yas;
 }

 public String getAd() {
  return ad;
 }

 public void setAd(String ad) {
  this.ad = ad;
 }

 public int getYas() {
  return yas;
 }

 public void setYas(int yas) {
  this.yas = yas;
 }

 public void yazdir() {
  System.out.println("PERSONEL\n" 
    + "ad\t:\t" + ad 
    + "\nyas\t:\t"+yas);
 }
}

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

 <bean id="pers1" class="test.Personel" scope="singleton">
  <property name="ad" value="enes"/>
  <property name="yas" value="25"/>
 </bean>
 
 <bean id="pers2" class="test.Personel" scope="prototype">
  <property name="ad" value="enes"/>
  <property name="yas" value="25"/>
 </bean>

</beans>

spring bean scopes


Yorumlar

Bu blogdaki popüler yayınlar

JSP Ders04 - JSP Direktifleri

SQL Ders27 - INSERT INTO

Java Mini Proje 005 - Dik Üçgenin Hipotenüsünü Bulma