Spring 문서
https://docs.spring.io/spring-framework/docs/3.0.0.M3/reference/html/ch04s04.html
오해한것 ?
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;
}
}
'Spring MVC, Sptring boot' 카테고리의 다른 글
[Spring Boot] 배포 어떤걸로? JAR or WAR (2) | 2021.08.12 |
---|---|
[AOP] 관점지향 프로그래밍 (도서참고) (2) | 2021.07.03 |
Spring DI / IoC 를 설명하자 (3) | 2021.06.19 |
[모델1, 모델2] Web - Java , Model 1, Model 2, Spring MVC (2) | 2021.06.10 |
[IoC , DI] 개념잡기 (0) | 2021.05.08 |