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

[Spring] ์• ๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ ๋นˆ ์„ค์ • ๋ฐฉ๋ฒ• ์ •๋ฆฌ

by Leica 2020. 4. 24.
๋ฐ˜์‘ํ˜•

[Spring] ์• ๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ ๋นˆ ์„ค์ • ๋ฐฉ๋ฒ• ์ •๋ฆฌ

๋นˆ ์„ค์ •์„ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์Šคํ”„๋ง ์• ๋…ธํ…Œ์ด์…˜ @Required, @Autowired, @Qualifier, @Value๊ณผ JSR-250 ์• ๋…ธํ…Œ์ด์…˜ @PostConstruct, @PreDestroy, @Resource์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž

์ด ์• ๋…ธํ…Œ์ด์…˜๋“ค์„ ์‚ฌ์šฉํ•ด ๊ธฐ์กด์— XML ๋นˆ ์„ค์ • ํŒŒ์ผ์— ๋ชจ๋‘ ์ž‘์„ฑํ•˜๋˜ ๋นˆ ์„ค์ •์„ ๋นˆ ํด๋ž˜์Šค์— ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

XML ์„ค์ •์— ์• ๋…ธํ…Œ์ด์…˜ ๋นˆ ์„ค์ •์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ ์ถ”๊ฐ€

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
</beans>

context ๋„ค์ž„ ์ŠคํŽ˜์ด์Šค์™€ <context:annotation-config/> ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด ๋นˆ ์„ค์ •์„ XML ํŒŒ์ผ์ด ์•„๋‹Œ ๋นˆ ํด๋ž˜์Šค์˜ ์• ๋…ธํ…Œ์ด์…˜์„ ๊ฒ€์ƒ‰ํ•ด ๋ฐ˜์˜ํ•œ๋‹ค.

 

@Required

- setter์— ๋ถ™์—ฌ ๋ฐ˜๋“œ์‹œ ์ฃผ์ž…ํ•ด์•ผํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋กœ ์„ค์ •ํ•˜๋Š” ์• ๋…ธํ…Œ์ด์…˜
- Spring 5.1 ๋ฒ„์ „ ๋ถ€ํ„ฐ Deprecated ๋˜์—ˆ๋‹ค. โžก๏ธ ๋ฐ˜๋“œ์‹œ ์ฃผ์ž…ํ•ด์•ผ ํ•  ํ”„๋กœํผํ‹ฐ๋Š” ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์ด์šฉํ•œ๋‹ค.
- ์Šคํ”„๋ง 5.1์ด์ƒ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ž๋ฐ” ํŒŒ์ผ๋กœ bean์„ ๋“ฑ๋กํ–ˆ์„ ๊ฒฝ์šฐ ๋ฌด์‹œ๋œ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ

public class TestBean1 {
	
	private int data1;

	public int getData1() {
		return data1;
	}
	
	// ํ•„์ˆ˜ ์ฃผ์ž… ํ”„๋กœํผํ‹ฐ
        // ์Šคํ”„๋ง 5.1์ด์ƒ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ ์ž๋ฐ” ํŒŒ์ผ๋กœ bean์„ ๋“ฑ๋กํ–ˆ์„ ๊ฒฝ์šฐ ๋ฌด์‹œ๋œ๋‹ค
	@Required
	public void setData1(int data1) {
		this.data1 = data1;
	}
}

@Configuration
public class BeanConfigClass {

	@Bean
	public TestBean1 testBean1() {
		return new TestBean1();
	}
}

 

@Autowired

- ๊ฐ์ฒด ํƒ€์ž…์„ ํ†ตํ•ด ๋นˆ ๊ฐ์ฒด๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•œ๋‹ค.
- ํ•„๋“œ, ์ƒ์„ฑ์ž, setter์— ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค.
- ํ•„๋“œ, setter์— ๋ถ™์—ฌ์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.
- ํ•„๋“œ์— ๋ถ™์ด๋ฉด setter๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋˜๋ฉฐ setter๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ์ปดํŒŒ์ผ ๊ณผ์ •์—์„œ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€๋œ๋‹ค.

 

์˜ˆ์ œ ์ž๋ฐ” ์ฝ”๋“œ

public class TestBean1 {

	// ํ•„๋“œ ์ž๋™ ์ฃผ์ž…
	// ์ž๋™์œผ๋กœ setter ๋ฉ”์„œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์–ด setter ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ฃผ์ž…๋œ๋‹ค.
	@Autowired
	private DataBean1 data3;
}

public class DataBean1 { }

 

์„ค์ • - ์ž๋ฐ”

@Configuration
public class BeanConfigClass {

	@Bean
	public TestBean1 testBean1() {
		return new TestBean1();
	}
	
	@Bean
	public DataBean1 dataBean1() {
		return new DataBean1();
	}
}

 

์„ค์ • - 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"
	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">
	                    
	<context:annotation-config/>
	
	<bean id='testBean1' class='com.atoz_develop.beans.TestBean1'/>
	<bean class='com.atoz_develop.beans.DataBean1'/>
	
</beans>

 

@Qualifier

- @Autowired์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค.
- @Autowired๋ฅผ ํ†ตํ•œ ์ž๋™ ์ฃผ์ž… ์‹œ ๊ฐ™์€ ํƒ€์ž…์˜ ๋นˆ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉด @Qualifier์— ์„ค์ •๋˜์–ด ์žˆ๋Š” ๋นˆ์„ ์ฐพ์•„ ์ฃผ์ž…ํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž๋ฐ” ์ฝ”๋“œ

public class TestBean1 {

	@Autowired
	@Qualifier("obj4")
	private DataBean2 data4;
	
	@Autowired
	@Qualifier("obj5")
	private DataBean2 data5;
}

public class DataBean2 { }

 

์„ค์ • - ์ž๋ฐ”

@Configuration
public class BeanConfigClass {

	@Bean
	public TestBean1 testBean1() {
		return new TestBean1();
	}
	
	@Bean
	public DataBean2 obj4() {
		return new DataBean2();
	}
	
	@Bean
	public DataBean2 obj5() {
		return new DataBean2();
	}
}

 

์„ค์ • - 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"
	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">
	                    
	<context:annotation-config/>
	
	<bean id='testBean1' class='com.atoz_develop.beans.TestBean1'/>
	<bean id='obj4' class='com.atoz_develop.beans.DataBean2'/>
	<bean id='obj5' class='com.atoz_develop.beans.DataBean2'/>

</beans>

 

@Value

- ์ƒ์„ฑ์ž ์ฃผ์ž… ์‹œ ์ž๋™์œผ๋กœ ์ฃผ์ž…๋˜์ง€ ์•Š๋Š” ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•๊ณผ ๋ฌธ์ž์—ด์˜ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž๋ฐ” ์ฝ”๋“œ

public class TestBean2 {
	
	private int data1;
	private String data2;
	private DataBean3 data3;
	private DataBean4 data4;

	public TestBean2(@Value("100") int data1, @Value("๋ฌธ์ž์—ด") String data2, DataBean3 data3, DataBean4 data4) {
		this.data1 = data1;
		this.data2 = data2;
		this.data3 = data3;
		this.data4 = data4;
	}
}

public class DataBean3 { }

public class DataBean4 { }

 

์„ค์ • - 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"
	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">

	<context:annotation-config/>
	
	<bean id='testBean2' class='com.atoz_develop.beans.TestBean2'/>
	
	<bean class='com.atoz_develop.beans.DataBean3'/>
	<bean class='com.atoz_develop.beans.DataBean4'/>
	
</beans>

 

XML ์„ค์ •์—์„œ <context:annotation-config/>๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋นˆ ํด๋ž˜์Šค์— ์ฃผ์ž… ์„ค์ •์„ ๋”ฐ๋กœ ํ•˜์ง€ ์•Š์•„๋„ ์ƒ์„ฑ์ž ์ฃผ์ž…์ด ์ž๋™์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

์ด๋•Œ ๊ธฐ๋ณธ ํƒ€์ž…๊ณผ ๋ฌธ์ž์—ด ํƒ€์ž…์˜ ํ•„๋“œ๋Š” ์ž๋™์œผ๋กœ ์ฃผ์ž…๋˜์ง€ ์•Š์œผ๋ฉฐ @Value๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ž๋ฐ” ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •(@Configuration)์„ ์‚ฌ์šฉํ• ๋•Œ๋Š” ์ด ๋ฐฉ๋ฒ•์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

JSR-250 ์• ๋…ธํ…Œ์ด์…˜ ์˜์กด์„ฑ ์ถ”๊ฐ€

<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

 

@PostConstruct

- @Bean(initMethod="...") ๋Œ€์‹  ์‚ฌ์šฉ
- ์ƒ์„ฑ์ž ํ˜ธ์ถœ ํ›„ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋  ๋ฉ”์†Œ๋“œ์— ๋ถ™์—ฌ์„œ ์‚ฌ์šฉ

@Configuration ํด๋ž˜์Šค์—์„œ @Bean์˜ initMethod ์†์„ฑ์— ๋นˆ์ด ์ƒ์„ฑ๋ ๋•Œ ํ˜ธ์ถœ๋  ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋Œ€์‹  ๋นˆ์˜ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์— @PostConstruct๋ฅผ ๋ถ™์—ฌ ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ

public class TestBean2 {

	// ์ƒ์„ฑ์ž ํ˜ธ์ถœ ์ดํ›„ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ
	@PostConstruct
	public void init2() {
		System.out.println("TestBean2์˜ init ๋ฉ”์„œ๋“œ");
	}
}

 

์„ค์ •

@Configuration
public class BeanConfigClass {
	
	@Bean
	public TestBean2 obj2() {
		return new TestBean2();
	}
}

 

@PreDestroy

- @Bean(destroyMethod="...") ๋Œ€์‹  ์‚ฌ์šฉ
- ๊ฐ์ฒด ์†Œ๋ฉธ ์ „ ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋  ๋ฉ”์†Œ๋“œ์— ๋ถ™์—ฌ์„œ ์‚ฌ์šฉ

@Configuration ํด๋ž˜์Šค์—์„œ @Bean์˜ destroyMethod ์†์„ฑ์— ๋นˆ์ด ์ƒ์„ฑ๋ ๋•Œ ํ˜ธ์ถœ๋  ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋Œ€์‹  ๋นˆ์˜ ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ์— @PostConstruct๋ฅผ ๋ถ™์—ฌ ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์ œ ์ฝ”๋“œ

public class TestBean2 {
	
	// ๊ฐ์ฒด๊ฐ€ ์†Œ๋ฉธ๋˜๊ธฐ ์ „์— ์ž๋™์œผ๋กœ ํ˜ธ์ถœ
	@PreDestroy
	public void destroy2() {
		System.out.println("TestBean2์˜ destroy ๋ฉ”์„œ๋“œ");
	}
}

 

์„ค์ •

@Configuration
public class BeanConfigClass {
	
	@Bean
	public TestBean2 obj2() {
		return new TestBean2();
	}
}

 

@Resource

- ๋นˆ ์ด๋ฆ„(id)์„ ํ†ตํ•ด ์ž๋™ ์ฃผ์ž…
- @Autowired + @Qualifier์™€ ์œ ์‚ฌ
- ํ•„๋“œ๋ช…๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋นˆ์„ ์ฃผ์ž…
- ํ•„๋“œ๋ช…๊ณผ ๋นˆ ์ด๋ฆ„์ด ๋‹ค๋ฅด๋ฉด @Resource(name=“…”)

 

๐Ÿ‘‰ ํ•„๋“œ๋ช…๊ณผ ๋นˆ ์ด๋ฆ„(id)๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ

์˜ˆ์ œ ์ฝ”๋“œ

public class TestBean5 {
	
	// ๋ณ€์ˆ˜์˜ ์ด๋ฆ„๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์˜ Bean์ด ์ฃผ์ž…๋œ๋‹ค.
	@Resource
	private DataBean1 data1;
	
	@Resource
	private DataBean2 data2;
}

 

์„ค์ •

@Configuration
public class BeanConfigClass {
	
	@Bean
	public DataBean1 data1() {
		return new DataBean1();
	}
	
	@Bean
	public DataBean2 data2() {
		return new DataBean2();
	}
	
	@Bean
	public TestBean5 obj5() {
		return new TestBean5();
	}
}

 

๐Ÿ‘‰ ํ•„๋“œ๋ช…๊ณผ ๋นˆ ์ด๋ฆ„(id)๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ - name ์†์„ฑ ์‚ฌ์šฉ

์˜ˆ์ œ ์ฝ”๋“œ

public class TestBean6 {
	
	@Resource(name = "data1")
	private DataBean1 data100;

	@Resource(name = "data2")
	private DataBean2 data200;
}

 

์„ค์ •

@Configuration
public class BeanConfigClass {
	
	@Bean
	public DataBean1 data1() {
		return new DataBean1();
	}
	
	@Bean
	public DataBean2 data2() {
		return new DataBean2();
	}
	
	@Bean
	public TestBean6 obj6() {
		return new TestBean6();
	}
}

TestBean6์˜ DataBean1ํƒ€์ž… ํ•„๋“œ๋ช…์ด ๊ฐ๊ฐ data100, data200์ด๊ณ  ๋นˆ ๋“ฑ๋ก์€ data1, data2๋กœ ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋ƒฅ @Resource๋ฅผ ๋ถ™์ด๋ฉด ์ž๋™ ์ฃผ์ž…์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.

์ด๋Ÿฐ ๊ฒฝ์šฐ @Resource์˜ name ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ๋นˆ ์ด๋ฆ„์„ ์ง์ ‘ ์ง€์ •ํ•œ๋‹ค.

 

Java์˜ annotation-api๋Š” ์Šคํ”„๋ง์„ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ž๋ฐ” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

์Šคํ”„๋ง์—์„œ๋„ @PostConstruct, @PreDestroy, @Resource์™€ ๊ฐ™์€ ์• ๋…ธํ…Œ์ด์…˜์„ ์ง€์›ํ•˜๋ฏ€๋กœ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€