d. Bean Scopes - kimxavi/spring_tutorial GitHub Wiki
์คํ๋ง์์ ์ ์ ์ํ ๋, scope์ ๋ํ ์ ์ธ์ ํ ์ ์๋ค. ์๋ฅผ ๋ค์ด, ์คํ๋ง์์ ์๋ก์ด bean์ ์ธ์คํด์ค๋ฅผ ํ๋๊ฐ ํ์ํ ๋๋ง๋ค ์์ฐํ๋ ๊ฒ์ bean์ scope ์์ฑ์ prototype ์ผ๋ก ์ ์ธํ๋ค. ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ์ bean ์ธ์คํด์ค๋ฅผ ํ์ํ ๋๋ง๋ค ๋ฐํ ๋ฐ๋๋ก ํ๋ ค๋ฉด scope๋ฅผ singleton๋ก ์ ์ธํ๋ค. ๋ค์ฏ ๊ฐ์ง์ ์์ฑ์ด ์๋๋ฐ ๊ทธ ์ค์ ์ธ ๊ฐ์ง๋ web-aware ApplicationContext๋ฅผ ์ฌ์ฉํ ๋๋ง ์ธ ์ ์๋ค.
- singleton : Spring IoC ์ปจํ ์ด๋๋ง๋ค ํ๋์ ์ธ์คํด์ค.(default)
- prototype : ์ผ๋ง๋ ์ง ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ์์ฑํด์ ๊ฐ์ง ์ ์๋ค.
- request : HTTP ์์ฒญ์ ์ ์๋๋ค.
- session : HTTP ์ธ์ .
- global-session : global HTTP ์ธ์ .
๋ง์ฝ scope๊ฐ singleton์ผ๋ก ์ค์ ๋๋ฉด, ์คํ๋ง IoC ์ปจํ ์ด๋๋ ์ ํํ ํ๋์ ๊ฐ์ฒด ์ธ์คํด์ค๋ง์ ์์ฑํ๋ค. ์ด ํ๋์ ์ธ์คํด์ค๋ singleton beans ์ ์บ์๋ก ์ ์ฅ๋๋ค ๊ทธ๋ฆฌ๊ณ ๊ทธ ๋ค์์ ๋ชจ๋ ์์ฒญ๊ณผ ์ฐธ์กฐ๋ค์ ์บ์๋ ๊ฐ์ฒด๊ฐ ๋ฐํ๋ฉ๋๋ค.
<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
<!-- collaborators and configuration for this bean go here -->
</bean>
HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.setMessage("I'm object A");
objA.getMessage();
HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
}
}
- ํ ๋ฒ ๊ฐ์ ์ธํ ํ ๊ฒ์ด ๋ค์์ bean์ ๊ฐ์ ธ์์ ๋๋ ๊ทธ๋๋ก ์ค์ ๋์ด ์๋ค.
Beans.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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
scope="singleton">
</bean>
</beans>
Your Message : I'm object A
Your Message : I'm object A
๋ง์ฝ scope๊ฐ prototype๋ก ์ค์ ๋์ด ์๋ค๋ฉด, ์คํ๋ง IoC ์ปจํ ์ด๋๋ ์์ฒญ์ด ์ฌ ๋๋ง๋ค ๊ฐ์ฒด์ bean ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. state-full ์ prototype์ด ์ฐ์ด๊ณ stateless๋ singleton์ผ๋ก ์ฌ์ฉํ๋ค.
HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
objA.setMessage("I'm object A");
objA.getMessage();
HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
objB.getMessage();
}
}
Beans.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-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
scope="prototype">
</bean>
</beans>
Your Message : I'm object A
Your Message : null