๐ŸŒ WEB/Spring

Spring ์ด์ •๋ฆฌ 3.Annotation

์• ์ •์“ฐ 2020. 6. 13. 12:56

ํŠน์ง• ?

 

- Spring Web MVC Framework๋Š” JAVA1.5๋ถ€ํ„ฐ Annotation์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

- xml์„ค์ •์ด ๋„ˆ๋ฌด ๊ธธ์–ด์ง์— ๋”ฐ๋ผ ๊ทธ ๋Œ€์•ˆ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ๋‹ค.

- ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ํ™œ์„ฑํ™”๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— xml์—์„œ ํ™•์„ฑํ™” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

 

์žฅ์  ?

 

- ์„ค์ •์ด ๊ฐ„ํŽธํ•ด ์ง, view ํŽ˜์ด์ง€์™€ ๊ฐ์ฒด ๋˜๋Š” ๋ฉ”์„œ๋“œ์˜ ๋งตํ•‘์ด ๋ช…ํ™•ํ•ด ์ง.


Annotation์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ค์ •์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

1) CommonAnnotationBeanPostProcessor์— ํด๋ž˜์Šค ์„ค์ •ํŒŒ์ผ์— ๋นˆ๊ฐ์ฒด๋กœ ๋“ฑ๋กํ•˜์—ฌ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์‚ฌ์šฉ

2) <context:annotation-config/> ํƒœ๊ทธ ์‚ฌ์šฉ : ์ด๋ฏธ ๋“ฑ๋ก๋œ ๋นˆ๋“ค์˜ ์–ด๋…ธํ…Œ์ด์…˜์„ ํ™œ์„ฑํ™”

3) <context:component-scan package="***"/> :

ํŠน์ • ํŒจํ‚ค์ง€ ์•ˆ์˜ ํด๋ž˜์Šค๋“ค์„ ์Šค์บ”ํ•˜๊ณ , Annotation์„ ํ™•์ธ ํ›„ bean ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑ

- ์Šคํ…Œ๋ ˆ์˜คํƒ€์ž…์˜ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ž๋™์œผ๋กœ ๋“ฑ๋ก ํ•œ๋‹ค ( Spring Framework๊ฐ€ ๊ด€๋ฆฌํ•ด ์ค€๋‹ค.)

 

 

๊ธฐ๋ณธ Annotation์˜ ์ข…๋ฅ˜ 

- org.springframework.stereotype

 

@Compoenet : @Controller,@Service,@Repository ์˜ ์ตœ์ƒ์œ„ annotation์ด๋‹ค.

@Controller : Spring MVC์—์„œ Controller๋ฅผ ์ธ์‹ ์‹œ์ผœ์ค€๋‹ค.

@Service : Business ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ, ๋กœ์ง์„ ์‚ฌ์šฉํ•œ๋‹ค.

@Repository : ์ผ๋ฐ˜์  Data Access Object์—์„œ ์‚ฌ์šฉ, DB Exception์„ DataAccessExceiption

 


1. @Component

ํŒจํ‚ค์ง€ : org.springframework.stereotype

๋ฒ„   ์ „ : spring 2.5

์ง์ ‘ ์ž‘์„ฑํ•œ Class๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜.

 

2. @Autowired

ํŒจํ‚ค์ง€ : org.springframework.beans.factory.annotation

๋ฒ„   ์ „ : spring2.5

Spring์˜ "์˜์กด๊ด€๊ณ„"๋ฅผ ์ž๋™์œผ๋กœ ์„ค์ •ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

- ํ•ด๋‹น ํƒ€์ž…์˜ bena์ด ์กด์žฌ ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, 2๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ ( ํ•ด๊ฒฐ๋ฐฉ๋ฒ• : @Qualifer์‚ฌ์šฉ )

 


DI ์ฃผ์ž… ํ˜•ํƒœ 3๊ฐ€์ง€

1. Construcotr : ํ•„์š”ํ•œ ์˜์กด์„ฑ์„ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ์„ ์ฃผ์ž…

(์Šคํ”„๋ง 4.3๋ฒ„์ „ ์ดํ›„ ๊ถŒ์žฅ)

2. Field :Bean์œผ๋กœ ๋“ฑ๋ก๋œ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค์— Field๋กœ ์„ ์–ธํ•œ ๋’ค @Autowiredํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ์ž๋™์œผ๋กœ ์ฃผ์ž…๋œ๋‹ค.

3. Setter : ์˜์กด์„ฑ์„ ์ž…๋ ซ๋ฐ›๋Š” setter ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•œ๋‹ค.


3. @Qualifer("name1")

@Autowired ํ–ˆ์„ ๋•Œ ๋™์ผ ํƒ€์ž…์„ ๊ฐ€์ง„ bean๊ฐ์ฒด๊ฐ€ ๋‘๊ฐœ ์žˆ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ) 

private A obj

<bean id="obj1" class="a.b.c.A"><qualifier value="name1"></bean>

<bean id="obj2" class="a.b.c.A"></bean>

 

4. @Required

ํŒจํ‚ค์ง€ : org.springframework.beans.factory.annotation

๋ฒ„   ์ „ : spring 2.5

ํ•„์ˆ˜๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

bean์„ ๋งŒ๋“ค์–ด ๋‚ผ๋•Œ ํ•„์ˆ˜๋กœ ์ž…๋ ฅํ•ด์•ผ ๋˜๋Š” ๋ฉ”์„œ๋“œ ์•ž์—์„œ @Required๋ผ๊ณ  ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด

bean์„ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

 

5. @Repository

ํŒจํ‚ค์ง€ : org.springframework.stereotype

๋ฒ„   ์ „ : spring2.5

DAO์— ์‚ฌ์šฉ๋˜๊ณ , DB Exception์„ DataAccessException์ฒ˜๋ฆฌํ•œ๋‹ค.

 

6. @Service

Businessํด๋ž˜์Šค์—์„œ(๋กœ์ง) ์‚ฌ์šฉํ•œ๋‹ค 

 

Controller -> Service -> Repository -> DataBase

   ๋ถ„๊ธฐ          DAO์กฐํ•ฉ     ๊ธฐ๋Šฅ          DataBase

 

7. @Resource

 

JAVA1.6 / JEE5 ๋ฒ„์ „์— ์ถ”๊ฐ€ ๋˜์—ˆ๋‹ค.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ•„์š”๋กœ ํ•˜๋Š” ์ž์›์„ ์ž๋™์œผ๋กœ ์—ฐ๊ฒฐ ํ• ๋•Œ ์‚ฌ์šฉ,

name์†์„ฑ ์‚ฌ์šฉํ•ด์„œ bean ๊ฐ์ฒด ์ด๋ฆ„์„ ์—ฐ๊ฒฐํ•œ๋‹ค.

@Resource(name="TestDtoDao")

 

๋ฐ˜์‘ํ˜•