spring core : spring bean scope
This commit is contained in:
@@ -14,6 +14,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter'
|
||||
implementation 'javax.inject:javax.inject:1'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.basic.scope;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class PrototypeTest {
|
||||
|
||||
@Test
|
||||
void prototypeBeanFind() {
|
||||
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class);
|
||||
System.out.println("find prototypeBean1");
|
||||
PrototypeBean bean1 = ac.getBean(PrototypeBean.class);
|
||||
System.out.println("find prototypeBean2");
|
||||
PrototypeBean bean2 = ac.getBean(PrototypeBean.class);
|
||||
System.out.println("bean1 = " + bean1);
|
||||
System.out.println("bean2 = " + bean2);
|
||||
|
||||
assertThat(bean1).isNotSameAs(bean2);
|
||||
|
||||
ac.close();
|
||||
}
|
||||
|
||||
@Scope("prototype")
|
||||
static class PrototypeBean {
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("SingletonBean.init");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
System.out.println("SingletonBean.destroy");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.example.basic.scope;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SingletonTest {
|
||||
|
||||
@Test
|
||||
void singletonBeanFind() {
|
||||
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SingletonBean.class);
|
||||
SingletonBean bean1 = ac.getBean(SingletonBean.class);
|
||||
SingletonBean bean2 = ac.getBean(SingletonBean.class);
|
||||
System.out.println("bean1 = " + bean1);
|
||||
System.out.println("bean2 = " + bean2);
|
||||
|
||||
assertThat(bean1).isSameAs(bean2);
|
||||
|
||||
ac.close();
|
||||
}
|
||||
|
||||
@Scope("singleton")
|
||||
static class SingletonBean {
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("SingletonBean.init");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
System.out.println("SingletonBean.destroy");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.example.basic.scope;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.inject.Provider;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class SingletonWithPrototypeTest1 {
|
||||
|
||||
@Test
|
||||
void prototypeFind() {
|
||||
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class);
|
||||
PrototypeBean bean1 = ac.getBean(PrototypeBean.class);
|
||||
bean1.addCount();
|
||||
assertThat(bean1.getCount()).isEqualTo(1);
|
||||
|
||||
PrototypeBean bean2 = ac.getBean(PrototypeBean.class);
|
||||
bean2.addCount();
|
||||
assertThat(bean2.getCount()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void singletonClientUsePrototype() {
|
||||
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(PrototypeBean.class, ClientBean.class);
|
||||
|
||||
ClientBean bean1 = ac.getBean(ClientBean.class);
|
||||
int count1 = bean1.logic();
|
||||
assertThat(count1).isEqualTo(1);
|
||||
|
||||
ClientBean bean2 = ac.getBean(ClientBean.class);
|
||||
int count2 = bean2.logic();
|
||||
assertThat(count2).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Scope("singleton")
|
||||
static class ClientBean {
|
||||
// @Autowired
|
||||
// private PrototypeBean prototypeBean; // 생성시점에 주입
|
||||
|
||||
// @Autowired
|
||||
// private ObjectProvider<PrototypeBean> prototypeBeanProvider; // 스프링의 provider (Dependency Lookup)
|
||||
|
||||
@Autowired
|
||||
private Provider<PrototypeBean> prototypeBeanProvider; // java의 provider (Dependency Lookup)
|
||||
|
||||
public int logic() {
|
||||
// PrototypeBean prototypeBean = prototypeBeanProvider.getObject();
|
||||
PrototypeBean prototypeBean = prototypeBeanProvider.get();
|
||||
prototypeBean.addCount();
|
||||
return prototypeBean.getCount();
|
||||
}
|
||||
}
|
||||
|
||||
@Scope("prototype")
|
||||
static class PrototypeBean {
|
||||
private int count = 0;
|
||||
|
||||
public void addCount() {
|
||||
count++;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("PrototypeBean.init " + this);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
System.out.println("PrototypeBean.destroy");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user