[Spring] Annotation

ํ˜œ์ฟฐยท2023๋…„ 9์›” 5์ผ
0
post-thumbnail

๐Ÿ“Œ ๊ฐœ๋…

Annotation์ด๋ž€? ์–ด๋…ธํ…Œ์ด์…˜์˜ ์‚ฌ์ „์  ์˜๋ฏธ๋Š” ์ฃผ์„์ด๋ž€ ๋œป์œผ๋กœ, ์ž๋ฐ”์—์„œ์˜ Annotation์€ ์ฝ”๋“œ ์‚ฌ์ด์— ์ฃผ์„์ฒ˜๋Ÿผ ์“ฐ์ด๋ฉฐ ํŠน๋ณ„ํ•œ ์˜๋ฏธ, ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๋Š” ๊ธฐ์ˆ ์ด๋‹ค.
@xxx ํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•˜๋ฉฐ ์™ธ๋ถ€ ์†Œํ”„ํŠธ์›จ์–ด์— ํ•„์š”ํ•œ ์ฒ˜๋ฆฌ ๋‚ด์šฉ์„ ์ „๋‹ฌํ•œ๋‹ค.
DI๋Š” xml์— ์˜์กด์ ์ด๋‹ค. ํ•˜์ง€๋งŒ, Annotation์„ ์“ด๋‹ค๋ฉด xml์˜ ์—ญํ• ์•„ ์ถ•์†Œ๋œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ ๋ฐ ์ œ๊ฑฐ๊ฐ€ ํŽธ๋ฆฌํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. Annotation ๋ณด๋‹ค init.xml์ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋จผ์ €์ด๋‹ค.

annotation์„ ์‚ฌ์šฉํ•˜๊ธฐ์— ์•ž์„œ ์ถ”๊ฐ€ํ•ด์•ผํ•  ๋‚ด์šฉ์ด ์žˆ๋‹ค.
pom.xml

<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>javax.annotation-api</artifactId>
			<version>1.3.1</version>
</dependency>

init.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-4.3.xsd">
	<context:component-scan base-package="anno1_auto"/> <!-- ์‚ฌ์šฉํ•  ํŒจํ‚ค์ง€์˜ ์ด๋ฆ„ -->
</beans>

๐Ÿ“Œ ์ข…๋ฅ˜

์†Œ์Šค์ฝ”๋“œ - https://github.com/junghyeyoun/spring

๐Ÿ“ ๋ ˆ์ด์–ด๋ณ„๋กœ ์‚ฌ์šฉํ•  ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ์–ด๋…ธํ…Œ์ด์…˜

๋ ˆ์ด์–ด๋ ˆ์ด์–ด ๊ฐœ์š”์–ด๋…ธํ…Œ์ด์…˜์–ด๋…ธํ…Œ์ด์…˜ ๊ฐœ์š”
Application Layerํด๋ผ์ด์–ธํŠธ์™€์˜ ๋ฐ์ดํ„ฐ ์ž…์ถœ๋ ฅ์„ ์ œ์–ดํ•˜๋Š” ๋ ˆ์ด์–ด@Controller์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ ˆ์ด์–ด์˜ ์ปจํŠธ๋กค๋Ÿฌ์— ๋ถ€์—ฌ
Domain Layer์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ค‘์‹ฌ์ด ๋˜๋Š” ๋ ˆ์ด์–ด๋กœ์„œ ์—…๋ฌด ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ ˆ์ด์–ด@Service๋„๋ฉ”์ธ ๋ ˆ์ด์–ด์˜ ์—…๋ฌด์ฒ˜๋ฆฌ์— ๋ถ€์—ฌ
Infrastructure Layer๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ์˜์†์„ฑ๋“ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ ˆ์ด์–ด@Repository์ธํ”„๋ผ ๋ ˆ์ด์–ด์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—‘์„ธ์Šค ์ฒ˜๋ฆฌ์— ๋ถ€์—ฌ

๐Ÿ“ @Component

๋‹จ์ˆœํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” annotation์ด๋‹ค.
class ์ด๋ฆ„์ด Sender์ธ class๊ฐ€ @Component ์‚ฌ์šฉํ•œ๋‹ค๋ฉด,
Sender sender = new Sender(); ์™€ ๊ฐ™์€ ๋œป์ด๋‹ค. Sender ๊ฐ์ฒด๊ฐ€ Singleton type์œผ๋กœ ๋งŒ๋“ค์–ด ์ง„๋‹ค.
@Component("abc") ์ฒ˜๋Ÿผ class ๋‚ด์šฉ๊ณผ ์ƒ๊ด€์—†์ด ์ด๋ฆ„์„ ์ž„์˜๋กœ ์ •ํ•ด๋„ ๋œ๋‹ค. ์ ์ง€ ์•Š์„ ๊ฒฝ์šฐ class ์ด๋ฆ„์œผ๋กœ ์ง€์ •๋œ๋‹ค. (โ— ์•ž๊ธ€์ž๋Š” ์†Œ๋ฌธ์ž๋กœ ์ž…๋ ฅ๋จ)

๐Ÿ“ @Autowired & @Qualifier

@Service
public class SenderProcess {
	@Autowired
	@Qualifier("sender") 
	private SenderInter inter;
	
	public SenderInter getInter() {
		return inter;
	}
	
	public void displayData() {
		inter.show();
	}
	
}

๋‹ค์Œ์€ @Autowired์™€ @Qualifier ํ™œ์šฉํ•œ ์ฝ”๋“œ์ด๋‹ค. Sender๋ผ๋Š” ๋นˆ์„ ์ฃผ์ž…ํ•ด ์™ธ๋ถ€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ“Ž @Autowired

ํด๋ž˜์Šค์˜ ํฌํ•จ ๊ด€๊ณ„๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•„์š”ํ•œ ์˜์กด ๊ฐ์ฒด์˜ โ€œํƒ€์ž…"์— ํ•ด๋‹นํ•˜๋Š” ๋นˆ์„ ์ฐพ์•„ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
์ƒ์„ฑ์ž, setter, ํ•„๋“œ 3๊ฐ€์ง€์˜ ๊ฒฝ์šฐ ํ•ด๋‹นํ•  ๋•Œ @Autowired๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ“Ž @Qualifier

type์ด ๋‘๊ฐœ ์ด์ƒ์ผ๋•Œ๋Š” ๋งคํ•‘ํ•˜๋Š” ๊ณผ์ •์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— @Qualifier๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด์˜ ์ด๋ฆ„์„ ์ •ํ™•ํ•˜๊ฒŒ ์ž…๋ ฅํ•ด์„œ ๋งคํ•‘ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ“ @Resource

@Service("ap")
public class AbcProcess { 
	@Resource
	private Abc1 abc1;
	
	@Resource(name="kbs")
	private Abc2 abc2;
}

@Resource๋ˆˆ ์ด๋ฆ„์— ์˜ํ•œ ๋งคํ•‘์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์ด @Resource๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, abc2์€ kbs์˜ ์ด๋ฆ„์„ ๊ฐ€์ง„ Bean ๊ฐ์ฒด์— ์˜์กด์„ฑ์„ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.
abc1๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์™€ ๊ฐ™์ด name ์†์„ฑ์„ ์ƒ๋žตํ•˜๋ฉด, @Resource ์–ด๋…ธํ…Œ์ด์…˜์ด ์ ์šฉ๋œ ํ•„๋“œ๋‚˜ ์„ค์ • ๋ฉ”์„œ๋“œ์˜ ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ๋‹ค.

๐Ÿ“ @Value

@Value๋Š” ์• ๋…ธํ…Œ์ด์…˜์ด ํ•„๋“œ๋‚˜ ๋ฉ”์„œ๋“œ(ํ˜น์€ ์ƒ์„ฑ์ž)์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜์ค€์—์„œ ํ‘œํ˜„์‹ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฐ’์„ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ์• ๋…ธํ…Œ์ด์…˜์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ SpEL(Spring Expression Language)(ex: #{ํ‘œํ˜„์‹})์ด๋‚˜ ์Šคํƒ€์ผ ์†์„ฑ ์ž๋ฆฌํ‘œ์‹œ์ž(ex: ${ํ‘œํ˜„์‹})๋กœ ๊ฐ’์„ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ ๋ฐฐ์—ด ๋“ฑ๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
์ด ์• ๋…ธํ…Œ์ด์…˜์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ์€ BeanPostProcessor ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์—์„œ ์ฒ˜๋ฆฌ๋œ๋‹ค.

public class MyProcess {
	@Value("#{dataInfo.name}") 
	private String name;
	private String part;
	
	public MyProcess() {
	}
	
	@Value("1,2,3,4,5") 
	private int arr[];
	
	@Value("#{dataInfo.job}")
	private String myJob;

์ฐธ๊ณ 
์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ ์ฒซ๊ฑธ์Œ - ์ฃผ์‹ํšŒ์‚ฌ ๋ฃจ๋„ค์Šค ํ‚ค๋…ธ์‹œํƒ€ ๋งˆ์‚ฌ์•„ํ‚ค์ง€์Œ ์ „๋ฏผ์ˆ˜ ์˜ฎ๊น€
https://velog.io/@gillog/Spring-Annotation-%EC%A0%95%EB%A6%AC#cacheput(annotation ๊ฐœ๋…)
https://catsbi.oopy.io/379afd0c-d956-4133-b585-20d13d823c1a(@Value)

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