๐Annotation
์ด๋ ธํ ์ด์ ์ ์ฌ์ ์ ์ผ๋ก๋ "์ฃผ์"์ด๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์๋ฐ ์ฝ๋์ @๋ฅผ ์ด์ฉํด ์ฃผ์์ฒ๋ผ ๋ฌ์ ํน์ํ ์๋ฏธ๋ฅผ ๋ถ์ฌํด์ฃผ๋ ์ญํ ์ ํ๋ค.
ํ๋ก๊ทธ๋จ์ ๊ดํ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ๊ณ , ์ฝ๋์ ์ ๋ณด๋ฅผ ์ถ๊ฐํ๋ ์ ํํ๋ ๋ฐฉ๋ฒ์ด๋ค.
๐ SpringBootApplication
Spring Boot๋ฅผ ์๋์ผ๋ก ์คํ์์ผ์ฃผ๋ ์ด๋ ธํ ์ด์
1) @ComponentScan์ ํตํด ์ปดํฌ๋ํธ๋ค์ Bean์ผ๋ก ๋ฑ๋กํ๋ค
2) @EnableAutoConfiguration์ ํตํด ๋ฏธ๋ฆฌ ์ ์ํด๋ ์๋ฐ ์ค์ ํ์ผ๋ค์ Bean์ผ๋ก ๋ฑ๋กํ๋ค.
* Spring Bean์ด๋
: Spring IOC ์ปจํ ์ด๋์ ์ํด ์์ฑ๋๊ณ , ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด ์๋ฐ ๊ฐ์ฒด๋ฅผ Bean ์ด๋ผ๊ณ ํจ
@Configuration
: ์คํ๋ง IOC ์ปจํ ์ด๋์๊ฒ ํด๋น ํด๋์ค๊ฐ Bean๊ตฌ์ฑ Class์์ ์๋ ค์ฃผ๋ ์ด๋ ธํ ์ด์
@ComponentScan
: @Component, @Service, @Repository, @Controller, @Configuration์ด ๋ถ์ ๋น๋ค์ ์ฐพ์์ Context์ ๋ฑ๋กํด ์ฃผ๋ ์ด๋ ธํ ์ด์
๐ Component
๊ฐ๋ฐ์๊ฐ ์ง์ ์์ฑํ Class๋ก Bean์ ๋ฑ๋กํ๊ธฐ ์ํ ์ด๋ ธํ ์ด์
@component ์ด๋ ธํ ์ด์ ์ Controller, Service, DAO ์ธ ์ข ๋ฅ์ ํด๋์ค์๋ง ์ฌ์ฉ์ ๊ถ์ฅ ํจ
: @component ๐ ๊ตฌ์ฒดํ ๐ @Controller, @Service, @Repository
@Controller
: Spring MVC์ Controller๋ก ์ฌ์ฉ๋๋ ํด๋์ค ์ ์ธ์ ๋จ์ํ ์์ผ์ฃผ๋ ์ด๋ ธํ ์ด์
@Service
: ๋น์ฆ๋์ค๋ก์ง์ด ๋ค์ด๊ฐ๋ Service๋ก ์ฌ์ฉ๋๋ ํด๋์ค์์ ์ ์ธํ๋ ์ด๋ ธํ ์ด์
@Repository
: DB์ ์ง์ ์ ์ผ๋ก ์ฐ๋๋๋ ์์ ์ ํ๋ DAOํด๋์ค์ ํนํ๋ ์ด๋ ธํ ์ด์
๐RequestMapping
ํด๋ผ์ด์ธํธ์๊ฒ ์์ฒญ๋ฐ๋ ์ฃผ์๋ฅผ ํด๋์ค์ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ์ด๋ ธํ ์ด์
์๋์ ์ด๋ ธํ ์ด์ ๋ค์ HttpMethods์ ๋งค์นญ๋์ด ์ฌ์ฉ๋๋ค. (Post, Get, Put, Delete, Patch)
@PostMapping
@GetMapping
@PutMapping
@DeleteMapping
@PatchMapping
๐RestController
@Controller + @ResponseBody์ ๊ฒฐํฉํํ์ ์ด๋ ธํ ์ด์ ์ผ๋ก,
ํด๋น ํด๋์ค๊ฐ ajax ์์ฒญ์ ๋ฐ์ Json/xmlํํ๋ก ๊ฐ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ ๋ ์ฌ์ฉ
*) @Controller์์ ์ฐจ์ด
- ๊ฐ์ฅ ํฐ ์ฐจ์ด์ ์ Http Response Body ์์ฑ ๋ฐฉ์์ ์ฐจ์ด์
: @RestController(์ฃผ์ฉ๋ ๋ฐ์ดํฐ ๋ฆฌํด)๋, ํด๋์ค ๋ฆฌํด ํ์ ์ด String์ด๋ฉด ๋ฌธ์์ด ๋ฐ์ดํฐ ์๋ฏธ
: @Controller(์ฃผ์ฉ๋ View๋ฆฌํด)๋, ํด๋์ค ๋ฆฌํดํ์ ์ด String์ด๋ฉด ๋ฆฌํด๊ฐ์ด jsp ํ์ผ๋ช ์ ์๋ฏธํจ
๐ ์คํํ๋ฆ
@Controller
Client -> Request -> Dispatcher Servlet -> Handler Mapping -> Controller -> View -> Dispatcher Servlet -> Response -> Client
@ResponseBody
Client -> Request -> Dispatcher Servlet -> Handler Mapping -> Controller (ResponseBody) -> Response -> Client
@RestController
Client -> HTTP Request -> Dispatcher Servlet -> Handler Mapping -> RestController (์๋ ResponseBody ์ถ๊ฐ)-> HTTP Response -> Client
๐ResponseBody / RequestBody
json ๋ฌธ๋ฒ ๊ตฌ์กฐ์ ํฌ๋งท์ผ๋ก ์ ์ก๋์ด์จ ๋ฐ์ดํฐ๋ฅผ ๋ด๋ณด๋ด๊ณ , ๊ฐ์ ธ์ฌ๋ ์ฌ์ฉํจ
@ResponseBody
: ์๋ฐ ๊ฐ์ฒด๋ฅผ HTTP ์์ฒญ์ Body ๋ด์ฉ์ผ๋ก ๋ณํ/๋งคํํ๋ ์ด๋ ธํ ์ด์
@RequestBody
: HTTP ์์ฒญ์ body ๋ด์ฉ์ ์ ๋ฌ๋ฐ์ ์๋ฐ ๊ฐ์ฒด๋ก ๋ณํ/๋งคํํ๋ ์ด๋ ธํ ์ด์
๐PathVariable
URL ๊ฒฝ๋ก์ฃผ์์ ์ฌ์ฉํ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๋ ์ด๋ ธํ ์ด์ (RESTful)
: @RequestMapping URL์ ์ ๋ถ๋ถ๊ณผ Method๋ด์ Parameter๋ถ๋ถ์ ์ ์๋ฅผ ํ์ฌ ์ฌ์ฉ์ด ๊ฐ๋ฅ
์ )
RequestParam : http://localhost:8080/?id=1
PathVariable : http://localhost:8080/1
'๐ฝ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring skill 01 | ์ธ์ฆ(Authentication)๊ณผ ์ธ๊ฐ(Authorization) (0) | 2022.12.04 |
---|---|
Spring | ์์์ฑ์ด ๋ญ๋ฐ (1) | 2022.12.01 |
Spring Introduction 03 | Rest ๊ทธ๋ฆฌ๊ณ Put/Patch (0) | 2022.11.30 |
Spring Introduction 02 | ์คํ๋ง ํด๋ ๊ตฌ์กฐ (1) | 2022.11.29 |
Spring | JPA ? (0) | 2022.11.28 |