๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปWEB BackEnd/ํ”„๋ ˆ์ž„์›Œํฌ Spring

๊ฐ์ฒด ์ƒ์„ฑ - (๋ฐฉ๋ฒ•2) ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ƒ์„ฑ

๋ชฉ์ฐจ

  • ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ƒ์„ฑ
  • Bean์˜ ๋ฒ”์œ„
  • ์„ค์ • ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ!

 

์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ์ƒ์„ฑ

1) src/main/resources/applicationContext.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">
		
    <bean id = "tWalk" class = "testPjt.TransportationWalk"/> //bean์œผ๋กœ ๊ฐ์ฒด ์ƒ์„ฑ๋จ
    
</beans>

 

2) MainClass ์ฝ”๋“œ ์ž‘์„ฑ

package testPjt;

import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {
	public static void main(String[] args) {
//		TransportationWalk transportationWalk = new TransportationWalk();
//		transportationWalk.move();
		
//		์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationContext.xml");
		
		TransportationWalk transportationWalk = ctx.getBean("tWalk", TransportationWalk.class);
		transportationWalk.move();
		
		ctx.close();
	}

}

 

๊ฒฐ๊ณผ ํ™”๋ฉด!

 

 

Bean์˜ ๋ฒ”์œ„

Singleton๋ฒ”์œ„ : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ƒ์„ฑ๋œ bean๊ฐ์ฒด์˜ ๊ฒฝ์šฐ ๋™์ผํ•œ ํƒ€์ž…์˜ ๋Œ€ํ•ด์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•œ ๊ฐœ๋งŒ ์ƒ์„ฑ์ด ๋˜๋ฉฐ, getBean() ๋ฉ”์†Œ๋“œ๋กœ ํ˜ธ์ถœ๋  ๋•Œ ๋™์ผํ•œ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

Prototype ๋ฒ”์œ„ : ์‹ฑ๊ธ€ํ†ค ๋ฒ”์œ„์˜ ๋ฐ˜๋Œ€ ๊ฐœ๋…์œผ๋กœ getBean()ํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

์Šคํ”„๋ง ์„ค์ • ํŒŒ์ผ์—์„œ bean๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ, scope์†์„ฑ์„ ๋ช…์‹œํ•ด์ค€๋‹ค.

 

 

์„ค์ • ํŒŒ์ผ์ด ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ!

์„ค์ • ํŒŒ์ผ์ด ๊ธธ์–ด์งˆ ๊ฒฝ์šฐ ํšจ์œจ์„ฑ์„ ์œ„ํ•ด์„œ ์—ฌ๋Ÿฌ ์„ค์ •ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. 

์ด๋Ÿด ๊ฒฝ์šฐ์—๋Š” ์–ด๋–ป๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์„๊นŒ? 

 

๋ฐฉ๋ฒ•1) stringํ˜•ํƒœ์˜ context

package testPjt;

import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {
	public static void main(String[] args) {
//		์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ• => stringํ˜•ํƒœ์˜ context
		String [] appCtxs = {"classpath:appCtx1.xml", "classpath:appCtx2.xml", classpath:appCtx3.xml} 
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(appCtxs);
		
		TransportationWalk transportationWalk = ctx.getBean("tWalk", TransportationWalk.class);
		transportationWalk.move();
		
		ctx.close();
	}

}

 

๋ฐฉ๋ฒ•2) ํ•œ ์„ค์ •ํŒŒ์ผ์—์„œ ๋‹ค๋ฅธ ์„ค์ •ํŒŒ์ผ๋“ค์„ import

#MainClass

package testPjt;

import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {
	public static void main(String[] args) {
//		์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ• => ๋‹ค๋ฅธ ์„ค์ •ํŒŒ์ผ์„ importํ•˜๋Š” ์„ค์ •ํŒŒ์ผ๋งŒ ๋กœ๋“œ
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:appCtxImport.xml");
		
		TransportationWalk transportationWalk = ctx.getBean("tWalk", TransportationWalk.class);
		transportationWalk.move();
		
		ctx.close();
	}

}
#appCtxImport.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-3.1.xsd"> 
            
//  ์„ค์ •ํŒŒ์ผ import
    <import resource="classpath:appCtx2.xml">
    <import resource="classpath:appCtx3.xml">
    
</beans>