02. Bean ๋ฑ๋ก (XML)
ย ์คํ๋ง ํ๊ฒฝ์์๋ ๊ฐ์ฒด๋ฅผ Bean ์ด๋ผ๊ณ ํ๋ค.
ย Spring Bean ์ ๋ฑ๋กํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ ๊ฐ์ง๊ฐ ์๋๋ฐ, XML ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๊ณผ JAVA ๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋ค. ์ฐ์ XML ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์ ๐๐ปโโ๏ธ
1๏ธโฃ ย src/main/java
โ๏ธ Bean ๋ฑ๋ก ์ค์ต์ ์งํํ class ์ ๋ณด
2๏ธโฃ ย beans.xml
โ๏ธ Bean ๋ฑ๋ก ์ ์, xml ํ์ผ์ ์๋ ํ๊ทธ๋ฅผ ๋ถ์ฌ๋ฃ์ด์ผ ํ๋ค. โbeansโ ํ๊ทธ ์์ โbeanโ ํ๊ทธ๋ฅผ ๋ฃ์ด์ ๋ฑ๋กํ๋ฉด ๋๋ค.
<?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">
</beans>
- JSP ์ useBean ํ๊ทธ์ ๋น์ทํ ๋ฐฉ์์ผ๋ก Bean ๊ฐ์ฒด๋ฅผ ๋ฑ๋กํ๋ค. class ์๋ ๊ฐ์ฒด ์์ฑ์ ์ํ๋ ํด๋์ค์ ๊ฒฝ๋ก๋ฅผ ์ ์ด๋ฃ๊ณ , id ์๋ ๋ณ์๋ช ์ ์ ์ด๋ฃ๋๋ค.
๐ <bean class="beans.Cart" id="cart" />
- ๋ฑ๋กํ Bean ๊ฐ์ฒด๋ฅผ ๋ถ๋ฌ์ค๋ ค๋ฉด, (1) xml ํ์ผ์ ๋ก๋ฉํ๊ณ , (2) getBean() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
public class Main {
public static void main(String[] args) {
// (1)
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("config/beans.xml");
//(2)
Cart cart = ctx.getBean("cart", Cart.class);
}
}
๐ก xml ํ์ผ์ ๋ก๋ฉํ ๋ Bean ์ด ์์ฑ๋๋ฉฐ, getBean() ๋ฉ์๋๋ ์์ฑ๋ ๊ฐ์ฒด์ ์ฃผ์๊ฐ์ ๋ถ๋ฌ์ค๋ ์์ ์ ํ๋ค. ๋ํ Bean ์ Singleton ์ผ๋ก ์์ฑ๋๋ค.
3๏ธโฃ ย lazy-init
โ๏ธ ๊ฐ์ฒด ์์ฑ ์๊ธฐ๋ฅผ ์กฐ์ ํ ์ ์๋ค. ์๋ฌด๋ฐ ์ค์ ์ ์ฃผ์ง ์์ผ๋ฉด xml ํ์ผ์ ๋ก๋ฉํ ๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋์ง๋ง, lazy-init = โtrueโ ๋ก ์ค์ ํ๋ฉด getBean() ๋ฉ์๋๋ก ํธ์ถํ ๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ค.
๐ฅ <bean class="beans.Avocado" id="avocado" lazy-init="true" />
4๏ธโฃ ย scope
โ๏ธ scope = โprototypeโ ์ผ๋ก ์ค์ ํ๋ฉด, ๊ฐ์ฒด ์์ฑ ์๊ธฐ๋ฅผ getBean() ๋ฉ์๋๋ก ํธ์ถํ ๋๋ก ๋ฆ์ถ ์ ์์ผ๋ฉด์ Singleton ์ผ๋ก ์์ฑ๋์ง ์๋๋ก ํ๋ค.
๐ <bean class="beans.Jumeokbap" id="jumeokbap" scope="prototype" />
5๏ธโฃ ย init-method, destroy-method
โ๏ธ Bean ์ ๋ฑ๋กํ ๋ init-method ๋ฅผ ์ค์ ํด๋๋ฉด ์์ฑ์๊ฐ ํธ์ถ๋ ์งํ init-method๋ก ์ค์ ํ ๋ฉ์๋๊ฐ ์คํ๋๋ค.
โ๏ธ ๋ง์ฐฌ๊ฐ์ง๋ก, destroy-method ๋ฅผ ์ค์ ํด๋๋ฉด ๊ฐ์ฒด๊ฐ ์๋ฉธํ๊ธฐ ์ง์ ์ destroy-method ๋ก ์ค์ ํ ๋ฉ์๋๊ฐ ์คํ๋๋ค.
๐ฎ <bean class="beans.Pudding" id="pudding" init-method="addCart" destroy-method="removeCart" />
6๏ธโฃ ย default-init-method, default-destroy-method
โ๏ธ xml ํ์ผ beans ํ๊ทธ ์์ ์ค์ ํ๋ ๋ฉ์๋๋ก, xml ํ์ผ์ ๋ก๋ฉํ ๋ ์๋์ผ๋ก ์คํ๋๊ณ , ์ข ๋ฃํ ๋ ์๋์ผ๋ก ์คํ๋๋ ๋ฉ์๋๋ฅผ ์ค์ ํ ์ ์๋ค.
- default ๋ฉ์๋ ์ค์
ย ย ย ย ๐ addCart() ๋ ์ฃผ๋จน๋ฐฅ ํด๋์ค์ ๋ฉ์๋
ย ย ย ย ๐ purchase() ๋ ์นดํธ ํด๋์ค์ ๋ฉ์๋
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-init-method="addCart" default-destroy-method="purchase">
<bean class="beans.Cart" id="cart" />
<bean class="beans.Jumeokbap" id="jumeokbap" autowire="byName">
<constructor-arg value="1" type="int"/>
<constructor-arg value="30์ด๋ง ๋ฐ์์ฃผ์ธ์"/>
</bean>
</beans>
- ์ฃผ๋จน๋ฐฅ ํด๋์ค์ Bean ์ jumeokbap ์ผ๋ก ๋ฑ๋กํ๊ณ ๋ก๋ฉํ๋ฉด ์๋์ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ์ถ๋ ฅ๋๋ค.
09:30:53.148 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'jumeokbap'
09:30:53.151 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'cart'
Jumeokbap 1๊ฐ๋ฅผ ์ฅ๋ฐ๊ตฌ๋์ ๋ฃ์์ต๋๋ค.
09:30:53.157 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@4681c175, started on Fri May 03 09:30:53 KST 2024
4000์ ๊ฒฐ์ ๋์์ต๋๋ค.