Spring_Bean

zooyeongยท2023๋…„ 6์›” 8์ผ
0

19์ฃผ์ฐจ

๋ชฉ๋ก ๋ณด๊ธฐ
2/4
post-thumbnail

๐Ÿ“Œ์Šคํ”„๋ง ๋นˆ(Spring Bean)?

์Šคํ”„๋ง ๋นˆ์— ๋“ค์–ด๊ฐ€๊ธฐ์— ์•ž์„œ ์Šคํ”„๋ง์˜ ๋Œ€ํ‘œ์ ์ธ 3๊ฐ€์ง€ ํŠน์„ฑ์„ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.

  • ์ œ์–ด์˜ ์—ญ์ „ IoC(Inversion of Control)
    IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์˜ ์ œ์–ด ํ๋ฆ„์— ๋Œ€ํ•œ ๊ถŒํ•œ์„ ๊ฐ€์ง„๋‹ค. ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋งŒ๋“ค๊ณ  ๋“ฑ๋กํ•œ bean๋“ค์„ ๊ด€๋ฆฌํ•œ๋‹ค.

  • ์˜์กด์„ฑ ์ฃผ์ž… DI(Dependency Injection)
    ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์‹œ์ ์— ์™ธ๋ถ€์—์„œ ์‹ค์ œ ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„์˜ ์‹ค์ œ ์˜์กด๊ด€๊ณ„๊ฐ€ ์—ฐ๊ฒฐ๋˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.

  • ๊ด€์  ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ AOP(Aspect Oriented Programming)
    ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๊ณตํ†ต์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๋ถ€๊ฐ€์ ์ธ ๊ธฐ๋Šฅ๋“ค์„ ๋…๋ฆฝ์ ์œผ๋กœ ๋ชจ๋“ˆํ™”ํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ์ด๋‹ค.

>>> ์—ฌ๊ธฐ์„œ Spring IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๋นˆ(Bean)์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.




๐Ÿ“Œ๋นˆ(Bean) ์ ‘๊ทผ๋ฐฉ๋ฒ•

@Controller
public class MainController implements BeanFactoryAware {
	
	SampleBean1 sb1;

	@RequestMapping("/checkBean")
	public String checkBean() {
		
		SampleBean1 bean1 = new SampleBean1();
		System.out.println(bean1);
		
		SampleBean1 bean2 = new SampleBean1();
		System.out.println(bean2);
		
		return "checkBean";
	}
}

โ†“ ์ฝ˜์†”๊ฒฐ๊ณผ

>>> ๋‹ค๋ฅธ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ!!



๐Ÿ’กApplicationContext ๊ตฌํ˜„

  • lazy-init >>> ํŠน์ • Bean์ด ๋Šฆ๊ฒŒ ์ดˆ๊ธฐํ™”๋˜๊ธฐ๋ฅผ ์›ํ•  ๋•Œ ์‚ฌ์šฉ
    defalut : ์„œ๋ฒ„ ์‹œ์ž‘ ์‹œ, xml ๋กœ๋”ฉ ๊ณผ์ •์—์„œ Bean ์ƒ์„ฑ
    true : ์‹ค์ œ Bean์— ์ ‘๊ทผํ•ด์„œ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ƒ์„ฑ
    false : ๊ธฐ๋ณธ ๋™์ž‘
    <bean id="sampleBean" class="com.app.beans.SampleBean" lazy-init="true" />

  • scope >>> defalut๋Š” ์‹ฑ๊ธ€ํ†ค ๊ฐ์ฒด!
    prototype : ์ผ๋ฐ˜์ ์ธ ๊ฐ์ฒด๋กœ ํ™œ์šฉ! (๊ฐ์ฒด ์ƒ์„ฑํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ž˜ ์“ฐ์ด์ง„ ์•Š์Œ)
    <bean id="sampleBean" class="com.app.beans.SampleBean" scope="prototype" />

  • init-method, destroy-method >>> Bean ์ƒ์„ฑ ํ›„ ์ฝœ๋ฐฑ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ ์„ ์–ธ
    <bean id="initDestroyBean" class="com.app.beans.InitDestroyBean" init-method="bean_init" destroy-method="bean_destroy" />




๐Ÿ’กApplicationContext ์ ‘๊ทผ

ApplicationContext๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•œ๋‹ค.
ApplicationContext๋Š” BeanFactory ์ธํ„ฐํŽ˜์ด์Šค์˜ ํ•˜์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋‹ค.
์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ๋นˆ์„ ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ๊ด€๋ฆฌํ•ด์ค€๋‹ค.

โ†“ root-context.xml

<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="sampleBean1" class="com.app.beans.SampleBean1" />

</beans>

โ†“ SampleBean1 ํด๋ž˜์Šค

public class SampleBean1 {
	
	public SampleBean1() {
		System.out.println("SampleBean1 ์ƒ์„ฑ์ž");
	}
    
}

โ†“ ์ปจํŠธ๋กค๋Ÿฌ

@Controller
public class BeanController {

	//์–ด๋…ธํ…Œ์ด์…˜ @Autowired๋กœ ์ ‘๊ทผ
	@Autowired
	ApplicationContext applicationContext;
    
	@RequestMapping("/sb1")
	public String sb1() {
		
		SampleBean1 sb1 = (SampleBean1)applicationContext.getBean("sampleBean1");
		System.out.println("/sb1 - " + sb1);
		
		return "checkBean";
	}
	
	@RequestMapping("/sb1_2")
	public String sb1_2() {
		SampleBean1 sb1 = (SampleBean1)applicationContext.getBean("sampleBean1");
		System.out.println("/sb1_2 - " + sb1);
		
		return "checkBean";
	}

}

โ†“ ์ฝ˜์†”์ฐฝ ์ถœ๋ ฅ๊ฒฐ๊ณผ

>>> ์‹คํ–‰๋„์ค‘ ์ƒ์„ฑ๋จ

>>> /sb1๊ณผ /sb1_2์— ์ ‘๊ทผํ•ด๋ณด๋ฉด, ์‹ฑ๊ธ€ํ†ค์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๊ฐ€ ๋™์ผํ•˜๊ฒŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ!!



์—ฐ์Šต๋ฌธ์ œ
ํŠน์ • URL ์ฃผ์†Œ๋กœ ์ ‘๊ทผ ํ•  ๊ฒฝ์šฐ์—, ๋“ฑ๋ก๋œ Bean ์— ์ ‘๊ทผํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ์ฝ˜์†” ์ฐฝ์— ์ถœ๋ ฅ๋˜๋„๋ก ์ž‘์„ฑ
(๋‹จ, ๋ชจ๋“  bean ๊ฐ์ฒด์˜ ์„ค์ •์€ xml bean ์„ค์ •์„ ํ†ตํ•ด์„œ ์ˆ˜ํ–‰ํ•˜๊ณ , ๊ฐ’ ํ™•์ธ์„ ์œ„ํ•ด ํ•„์š”ํ•œ Bean ๋“ค์€ Controller ์—์„œ ApplicationContext ๋ฅผ ํ†ตํ•˜์—ฌ ์ ‘๊ทผ)

localhost:8080/QuizBeanProject/coffeeList ๋กœ ์ ‘์†์‹œ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ.

localhost:8080/QuizBeanProject/coffeeAme ๋กœ ์ ‘์†์‹œ ์•„๋ฉ”๋ฆฌ์นด๋…ธ ์ถœ๋ ฅ
localhost:8080/QuizBeanProject/coffeeMoca ๋กœ ์ ‘์†์‹œ ์นดํŽ˜๋ชจ์นด ์ถœ๋ ฅ
>>>>> ํด๋ž˜์Šค <<<<<

  • class CupBean
    ํ•„๋“œ๋ณ€์ˆ˜ CoffeeBean coffeeBean;
  • class CoffeeBean
    ํ•„๋“œ๋ณ€์ˆ˜ name;



์šฐ์„  ์œ„์—์„œ ์š”๊ตฌํ•œ ํด๋ž˜์Šค ๋‘๊ฐœ๋ฅผ ๋จผ์ € ๋งŒ๋“ ๋‹ค.

โ†“ CoffeeBean ํด๋ž˜์Šค

public class CoffeeBean {

	String name;
	
	public CoffeeBean(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

โ†“ CupBean ํด๋ž˜์Šค

public class CupBean {

	CoffeeBean coffee;
	
	public CupBean(CoffeeBean coffee) {
		this.coffee = coffee;
	}

	public CoffeeBean getCoffee() {
		return coffee;
	}

	public void setCoffee(CoffeeBean coffee) {
		this.coffee = coffee;
	}
	
}

๋‘ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋‹ˆ ApplicationContext๋กœ bean์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด root-context.xml ํŒŒ์ผ์—์„œ ์ž‘์„ฑ์„ ์ด์–ด๊ฐ€๊ฒ ๋‹ค.

โ†“ root-context.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">
	
	<!-- constructor-arg value="" -->
  	<!-- CoffeeBean์œผ๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ์•„๋ฉ”๋ฆฌ์นด๋…ธ๋กœ ๊ฐ’ ๋‹ด๊ธฐ-->
	<bean id="ame" class="com.app.beans.CoffeeBean">
		<constructor-arg value="์•„๋ฉ”๋ฆฌ์นด๋…ธ" />
	</bean>
	
  	<!-- CoffeeBean์œผ๋กœ ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ์นดํŽ˜๋ชจ์นด๋กœ ๊ฐ’ ๋‹ด๊ธฐ-->
	<bean id="moca" class="com.app.beans.CoffeeBean">
		<constructor-arg value="์นดํŽ˜๋ชจ์นด" />
	</bean>
	
  
  	<!-- constructor-arg ref="" -->
  	<!-- ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ปต์— ๋‹ด๊ธฐ -->
	<bean id="coffeeBean1" class="com.app.beans.CupBean">
		<constructor-arg ref="ame" />
	</bean>
	
  	<!-- ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ปต์— ๋‹ด๊ธฐ -->
	<bean id="coffeeBean2" class="com.app.beans.CupBean">
		<constructor-arg ref="moca" />
	</bean>
	
</beans>

๊ฐ์ฒด ์ƒ์„ฑ ์‹œ ๊ฐ’์„ ๋‹ด์•„์ฃผ๋Š” ์ž‘์—…์„ ๋งˆ์ณค๋‹ค.
์ด์ œ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ bean์— ์ ‘๊ทผํ•ด ๋ถˆ๋Ÿฌ๋ณด๊ฒ ๋‹ค.

โ†“ ์ปจํŠธ๋กค๋Ÿฌ

@Controller
public class QuizbeanController {

	@Autowired
	ApplicationContext applicationContext;
	
	CupBean ame;
	CupBean moca;

	@RequestMapping("/coffeeList")
	public String coffeeList() {
		
		ame = (CupBean)applicationContext.getBean("coffeeBean1");
		moca = (CupBean)applicationContext.getBean("coffeeBean2");
		
		System.out.println("์ฒซ๋ฒˆ์งธ ์ปต์— ๋‹ด๊ธด ์Œ๋ฃŒ์˜ ์ด๋ฆ„ - " + ame.getCoffee().getName());
		System.out.println("๋‘๋ฒˆ์งธ ์ปต์— ๋‹ด๊ธด ์Œ๋ฃŒ์˜ ์ด๋ฆ„ - " + moca.getCoffee().getName());
		
		return "coffeeList";
	}
	
	@RequestMapping("/coffeeAme")
	public String coffeeAme() {
		
		ame = (CupBean)applicationContext.getBean("coffeeBean1");
		
		System.out.println("์ฒซ๋ฒˆ์งธ ์ปต์— ๋‹ด๊ธด ์Œ๋ฃŒ์˜ ์ด๋ฆ„ - " + ame.getCoffee().getName());
		
		return "coffeeList";
	}
	
	@RequestMapping("/coffeeMoca")
	public String coffeeMoca() {
		
		moca = (CupBean)applicationContext.getBean("coffeeBean2");
		
		System.out.println("๋‘๋ฒˆ์งธ ์ปต์— ๋‹ด๊ธด ์Œ๋ฃŒ์˜ ์ด๋ฆ„ - " + moca.getCoffee().getName());
		
		return "coffeeList";
	}
}



โ†“ ์‹คํ–‰๊ฒฐ๊ณผ

profile
Have a good day โŒฏโ€™โ–พโ€™โŒฏ

0๊ฐœ์˜ ๋Œ“๊ธ€