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>
  1. JSP ์˜ useBean ํƒœ๊ทธ์™€ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ Bean ๊ฐ์ฒด๋ฅผ ๋“ฑ๋กํ•œ๋‹ค. class ์—๋Š” ๊ฐ์ฒด ์ƒ์„ฑ์„ ์›ํ•˜๋Š” ํด๋ž˜์Šค์˜ ๊ฒฝ๋กœ๋ฅผ ์ ์–ด๋„ฃ๊ณ , id ์—๋Š” ๋ณ€์ˆ˜๋ช…์„ ์ ์–ด๋„ฃ๋Š”๋‹ค.
  ๐Ÿ›’ <bean class="beans.Cart" id="cart" />
  1. ๋“ฑ๋กํ•œ 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 ํŒŒ์ผ์„ ๋กœ๋”ฉํ•  ๋•Œ ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๊ณ , ์ข…๋ฃŒํ•  ๋•Œ ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

  1. 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>
  1. ์ฃผ๋จน๋ฐฅ ํด๋ž˜์Šค์˜ 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์› ๊ฒฐ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.