Notice
Recent Posts
Recent Comments
Tags
- dp 알고리즘
- TIL
- 장고 기초
- spring 기초
- 스프링 기초
- 브루트포스
- 코테 연습
- 프로그래머스
- BFS
- 코딩테스트 연습
- Django 기초
- 알고리즘 문제
- 알고리즘 공부
- programmers
- 다이나믹 프로그래밍
- 코테
- Spring 초보
- 99클럽 코테 스터디
- 백준 구현
- 프로그래머스 레벨1
- 백준 DFS와 BFS
- 스프링 초보
- 항해99 코테 스터디
- 백준
- 백준 다이나믹프로그래밍
- 항해99
- 프로그래머스 level1
- 코딩테스트
- 이분탐색
- 백준 dp
Archives
- Today
- Total
일일구름 IT
[Spring] bean의 범위 (Prototype) 본문
Bean의 범위
Spring에서 스프링 컨테이너에 생성된 Bean 객체는 getBean() 메소드로 호출될때 항상 같은 객체가 반환된다.
Bean 객체를 호출할때마다 다른 객체로 반환되기 위해서는 Bean 객체를 정의할때 scope 속성을 prototype으로 명시해주면 된다.
다음의 코드와 결과를 보며 싱글톤과 프로토타입의 차이를 확인해보자.
// MainClass.java 파일
public static void main(String[] args) {
GenericXmlApplication ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
InjectionBean injectionBean = ctx.getBean("injectionBean", InjectionBean.class);
DependencyBean dependencyBean01 = ctx.getBean("injectionBean", InjectionBean.class);
DependencyBean dependencyBean02 = ctx.getBean("injectionBean", InjectionBean.class);
if(dependencyBean01.equals(dependencyBean02)) {
System.out.println("dependencyBean01 == dependencyBean02");
} else {
System.out.println("dependencyBean01 != dependencyBean02");
}
// DependencyBean.java 파일
public class DependencyBean {
private InjectionBean injectionBean;
// 생성자
public DependencyBean(InjectionBean injectionBean) {
System.out.println("DependencyBean : constructor");
this.injectionBean = injectionBean;
}
// setter
public void setInjectionBean(InjectionBean injectionBean) {
System.out.println("DependencyBean: setter");
this.injectionBean = injectionBean;
}
싱글톤일 경우
// 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="injectionBear" class="scope.ex.InjectionBean" />
<bean id="dependencyBean" class="scope.exDependencyBean">
<constructor-arg ref="injectionBean" />
<property name="injectionBean" ref="injectionBean" />
</bean>
</beans>
결과
프로토타입일 경우
// 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="injectionBear" class="scope.ex.InjectionBean" />
<bean id="dependencyBean" class="scope.exDependencyBean" scope="prototype">
<constructor-arg ref="injectionBean" />
<property name="injectionBean" ref="injectionBean" />
</bean>
</beans>
결과
'Spring' 카테고리의 다른 글
[Spring] 생명주기 (Life Cycle) (2) | 2023.11.28 |
---|---|
[Spring] 의존객체 선택 (@Qualifier, @Named) (0) | 2023.11.11 |
[Spring] 의존객체 자동 주입 (@Autowired, @Resource, @Inject) (0) | 2023.11.09 |
[Spring] 스프링 설정 파일 분리 (문자열 배열, import) (0) | 2023.11.07 |
[Spring] 다양한 의존 객체 주입 (constructor-arg, property) (2) | 2023.10.29 |