Spring MVC, Sptring boot

Spring bean Scope - Singleton vs Prototype 에 대한 간단 테스트

고인돌개발자 2021. 9. 17. 22:10

Spring 문서 

https://docs.spring.io/spring-framework/docs/3.0.0.M3/reference/html/ch04s04.html

 

4.4 Bean scopes

The other scopes, namely request, session, and global session are for use only in web-based applications (and can be used irrespective of which particular web application framework you are using, if indeed any). In the interest of keeping related concepts

docs.spring.io

 

오해한것 ?

Prototype 은 bean 의 갯수가 늘어나서 각각 객체로 사용할 것이다.

 -> bean 은 여전히 하나만 생성이되고, 단 객체가 새로 생성이 된다.


Test Code 

 

1. Bean 등록 class | ServiceBeanScope.java

package com.spring.scope;

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

import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class ServiceBeanScope {
	
	private String name="홍길동";
	
	public ServiceBeanScope() {
		System.out.println("ServiceBeanScope - Constructor");
	}
	
	
	public String getName() {
		// TODO Auto-generated method stub
		return this.name;
	}


	public void setName(String name) {
		this.name = name;
		
	}
	
	/*
	@PostConstruct
    public void postConstruct() {
        System.out.println("ServiceTest - postConstruct");
    }
	
	@PreDestroy
    public void preDestroy() {
		System.out.println("preDestroy");
    }
    */

}

 

2. Controller Class

 

Main1.java

package com.spring.scope;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Main1 {

	@Autowired
	ServiceBeanScope serviceBeanScope;
	
	
	@RequestMapping("/main1")	
	public @ResponseBody String doTest(HttpServletRequest request) {
		
		//ServiceTest serviceTest = new ServiceTest();
		System.out.println("");
		System.out.println("Main1");
		System.out.println("serviceBeanScope  => "+serviceBeanScope);
		
		String name = serviceBeanScope.getName();		
		System.out.println("name =>"+name);
		
		
		String reName = request.getParameter("name");
		serviceBeanScope.setName(reName);
		
		name = serviceBeanScope.getName();		
		System.out.println("param =>"+name);		
		
		return name;
	}
}

Main2.java

package com.spring.scope;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Main2 {

	@Autowired
	ServiceBeanScope serviceBeanScope;
	
	@RequestMapping("/main2")
	public String doTest(HttpServletRequest request) {
		
		//ServiceTest serviceTest = new ServiceTest();
		System.out.println("");
		System.out.println("Main2");
		System.out.println("serviceBeanScope  => "+serviceBeanScope);
		
		String name = serviceBeanScope.getName();		
		System.out.println("name =>"+name);
		
		String reName = request.getParameter("name");
		serviceBeanScope.setName(reName);
		
		name = serviceBeanScope.getName();		
		System.out.println("param =>"+name);		
		
		return name;	
		
	}
}

Bean List 확인  beanlist.java

package com.spring.scope;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class beanlist {

	@Autowired
	DefaultListableBeanFactory df;
	
	@Autowired
    private ApplicationContext applicationContext;

	@RequestMapping("/mybean")
	public String myBean() {
		
		String strBean="";
		int count=0;
		
		for(String str : df.getBeanDefinitionNames()) {
			strBean += df.getBean(str).getClass().getName() + "<br>";  
			count++;
		}
		
		return "Total : "+count+"<p>"+strBean;
	}
	
	@RequestMapping("/mybean2")
    public String contextLoads() throws Exception {
		String strBean="";
		int count=0;
        if (applicationContext != null) {
            String[] beans = applicationContext.getBeanDefinitionNames();

            for (String bean : beans) {
                System.out.println("bean : " + bean);
                strBean += bean + "<br>";  
    			count++;
            }
        }
        
        return "Total : "+count+"<p>"+strBean;
        
    }
	
	
	
}