프로그래밍/Spring & Spring boot

09 Spring : @Scope("prototype")

pupu91 2022. 9. 1. 20:57
반응형

 

@Scope("prototype") 

: 기본적인 bean scope는 singleton으로 설정되어 있음

  singleton은 IoC 컨테이너 당 하나의 인스턴스만 생성함

  protorype으로 설정을 변경하면 요청할 때마다 bean 인스턴스(객체)를 새롭게 생성함.

 

 

@Scope("prototype")  ㅇㅖ제

 

  • 부모 class 생성
public abstract class Product { 
생성불가타입으로 만들어주기 abstract 
객체 생성하지 않을거여서~
	
	
	private String name;
	private int price;
	
	public Product() {}

	public Product(String name, int price) {
		super();
		this.name = name;
		this.price = price;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return  name + " "+ price; 
	}

	
}

 

 

 

  • 자식 class 생성

 

    Bread class 

public class Bread extends Product {
	
	private java.util.Date bakedDate;
	
	public Bread() {}
	
	

	public Bread(String name, int price, Date bakedDate) {
		super(name, price);
		this.bakedDate = bakedDate;
	}



	public java.util.Date getBakedDate() {
		return bakedDate;
	}

	public void setBakedDate(java.util.Date bakedDate) {
		this.bakedDate = bakedDate;
	}



	@Override
	public String toString() {
		return super.toString() + " " + bakedDate;
	}
	
	

}

 

 

 

    Beverage  class 

public class Beverage extends Product {

	
	private int capacity;
	
	public Beverage() {}
	
	public Beverage(String name, int price, int capacity) {
		super(name,price);
		this.capacity = capacity; 
	}

	public int getCapacity() {
		return capacity;
	}

	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	@Override
	public String toString() {
		return super.toString() + " " + capacity + "ml";
	} 
		
}

 

 

 

  • 아이템을 담을 ShoppingCart 생성 
public class ShoppingCart {

	private final List<Product> items;
	
	public ShoppingCart() {
		items = new ArrayList<>();
	}
	
	
	public void addItem(Product item) {
		items.add(item);
	}
	
	public List<Product> getItem(){
		return items;
	}
	
}

 

 

 

 

  • bean 등록
@Configuration
public class ContextConfiguration {
	
    @Bean
	public Product carpBread() {
		
		return new Bread("붕어빵", 1000, new java.util.Date());
	}
	
	@Bean
	public Product milk() {
		
		return new Beverage("딸기우유", 1500, 500);
	}
	
	@Bean
	public Product water() {
		
		return new Beverage("삼다수", 300, 350);
	}
	
	
	@Bean
	@Scope("prototype") // 기본 값 singleton에서 prototype으로 변경 
    public ShoppingCart cart() {
	      return new ShoppingCart();
	}
	
}

 

 

 

 

 

 

  • 출력하기
public class Application {

	public static void main(String[] args) {
		
	
		ApplicationContext context = new AnnotationConfigApplicationContext(ContextConfiguration.class);
		
		Product carpBread = context.getBean("carpBread", Bread.class);
		Product milk = context.getBean("milk", Beverage.class);
		Product water = context.getBean("water", Beverage.class);
		
		ShoppingCart cart1 = context.getBean("cart", ShoppingCart.class);
		cart1.addItem(carpBread);
		cart1.addItem(milk);
		
		
		System.out.println("cart1에 담긴 내용 : " + cart1.getItem());
		
		ShoppingCart cart2 = context.getBean("cart", ShoppingCart.class);
		cart2.addItem(water);
		
		System.out.println("cart2에 담긴 내용 : " + cart2.getItem());
		
		두 카트의 hashCode 출력
		System.out.println("cart1의 hashCode : " + cart1.hashCode());
		System.out.println("cart2의 hashCode : " + cart2.hashCode());
		
	}

}
출력 결과 
cart1에 담긴 내용 : [붕어빵 1000 Thu Sep 01 20:56:01 KST 2022, 딸기우유 1500 500ml]
cart2에 담긴 내용 : [삼다수 300 350ml]
cart1의 hashCode : 829149076
cart2의 hashCode : 1678046232
반응형