Annotation์ ์ฌ์ ์ ์๋ฏธ๋ก๋ ์ฃผ์์ด๋ผ๋ ๋ป์ด๋ค.
์๋ฐ์์ Annotation์ ์ฝ๋ ์ฌ์ด์ ์ฃผ์์ฒ๋ผ ์ฐ์ด๋ฉฐ ํน๋ณํ ์๋ฏธ, ๊ธฐ๋ฅ์ ์ํํ๋๋ก ํ๋ ๊ธฐ์ ์ด๋ค.
์์ฝ์ด ์์ @๋ฅผ ๋ถ์ฌ ์ฌ์ฉํ๋ค.
Spring์์๋ ๋ค์ํ ์ด๋ ธํ ์ด์ ์ ์ ๊ณตํ๋ค. ์ด๋ฌํ ์ด๋ ธํ ์ด์ ์ ํ๋, ๋ฉ์๋, ํด๋์ค์ ์ ์ฉ๋๋ฉฐ Bean ๋ฑ๋ก, ์์กด์ฑ์ฃผ์ , urlMapping, MVC ํจํด ์ ์ฉ๋ฑ์ ์ญํ ์ ํ๋ค.
์ฃผ์ Annotation
@SpringBootApplication : SpringBoot ์ดํ๋ฆฌ์ผ์ด์ ์ ์์ํ๋ main ํด๋์ค์ ์ ์ฉํ๋ ์ด๋ ธํ ์ด์ @ComponentScan, @EnableAutoConfiguration, @SpringBootConfiguration ๋ฑ์ ํฌํจํ๊ณ ์๋ค.
ํด๋น ์ด๋ ธํ ์ด์ ์ผ๋ก ์ธํด main Class ์ดํ ํจํค์ง์ ํฌํจ๋ ์ปดํฌ๋ํธ ๋๋ ํด๋์ค๋ค์ ์๋์ผ๋ก Bean ์ผ๋ก ๋ฑ๋ก ์์ผ์ค๋ค.@SpringBootApplication public class ApiTestApplication { public static void main(String[] args) { SpringApplication.run(ApiTestApplication.class, args); } }
@Configuration : @Configuration ์ด๋ ธํ ์ด์ ์ด ์ ์ฉ๋ ์ค์ ํด๋์ค๋ฅผ ์ค์บํ์ฌ ๋น ๊ตฌ์ฑ์ ๋ก๋ํ๊ณ ์ ํ๋ฆฌ์ผ์ด์ ์ปจํ ์คํธ์ ๋ฑ๋กํ๋ค.
@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
@Autowired : DI(Dependency Injection) ๋์์ผ๋ก ์ง์ , ํ๋, ์์ฑ์, setter ๋ฉ์๋์ ์ ์ฉํ์ฌ DI๋ฅผ ํ ์ ์๋ค.
@Service public class RestApiServiceLogic implements RestApiService { private final RestApiRepository apiRepository; @Autowired public RestApiServiceLogic(RestApiRepository apiRepository) { this.apiRepository = apiRepository; }
@Bean : @Configuration ๋ด๋ถ์์ ์ฌ์ฉ๋๋ฉฐ, ๋ฉ์๋ ๋จ์๋ก ์ฌ์ฉํ๋ค. ์ง์ ์ ์ด๊ฐ ๋ถ๊ฐ๋ฅํ ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฑ์ Bean์ผ๋ก ๋ง๋ค๋ คํ ๋ ์ฌ์ฉ๋๋ค. ์ฌ์ฉ์ ํด๋น ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ getter์ ๋น์ทํ ๋ฉ์๋๋ฅผ ๋ง๋ค๊ณ ๋ฆฌํด์ new๋ฅผ ๋ถ์ฌ ์์ฑ๊ณผ ๋ฆฌํดํ๋ค. Bean id๋ฅผ ์ง์ ํ์ง ์์ ๊ฒฝ์ฐ ๋ฉ์๋์ ์ด๋ฆ์ camelCase๋ก ๋ณ๊ฒฝํ๊ฒ์ด bean id๋ก ์ง์ ๋๋ค.
@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } }
@Component : ํด๋์ค๋ฅผ @ComponentScan์ ๋์์ผ๋ก ์ง์ ํ๋ค. ํด๋น ํด๋์ค๋ฅผ ์ค์บ๋๋ฉด ์๋์ผ๋ก Bean ๋ฑ๋ก์ด ๋๋ค.
@Component public class comTest { }
@ComponentScan : ์ค์บ์ ๋ํ ๋ฒ์๋ฅผ ์ง์ ํ๊ณ ํด๋น ํจํค์ง๋ฅผ ์ค์บํ์ฌ @Component ์ด๋ ธํ ์ด์ ์ด ๋ถ์ Class๋ค์ Bean ๋ฑ๋กํ๊ฑฐ๋ ์ ์ธ ์ํจ๋ค.
@Configuration @ComponentScan("com.api.") public class comScaner { }
@RequestMapping : ์์ฒญ URL๊ณผ ๋ฉํ๋๋ ๋ฉ์๋๋ฅผ ์ง์ ํ๋ ์ด๋ ธํ ์ด์
@RequestMapping("/logout") public void logout(){ }
๋ง์ฝ ํด๋์ค ๋ ๋ฒจ์ ์ ์ฉ์ํฌ ๊ฒฝ์ฐ ๊ณตํต URL ๊ฒฝ๋ก๋ฅผ ์ง์ ํ ์ ์๋ค.
@RestController @RequestMapping("/member") public class RestApiController { @RequestMapping("/logout") // "/member/logout" public void logout(){ }
@GetMapping : RequestMapping๊ณผ ์ ์ฌํ์ง๋ง Post, Put, Patch๋ฑ ๋ฉ์๋์ ๋ฐ๋ผ ๊ตฌ์ฒด์ ์ผ๋ก ์ง์ ํ ์ ์๋ค.
@GetMapping("/request") public String postmanTest(){ return ""; }
@Controller : URL mapping ๊ด๋ จ ์ด๋ ธํ ์ด์ ๋ฐ ๋ชจ๋ธ ๋ฐ ๋ทฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ ModelAndView ๋ฑ View์ Model์ ์๋ MVCํจํด์ค Controller ํด๋์ค์ ์ฌ์ฉ๋๋ค.
@Controller public class RestApiController { ... }
@ResponseBody : ๋ฉ์๋์ ๋ฐํ๊ฐ์ JSON, XML๋ก ํ ๋ ์ฌ์ฉํ๋ค. HTTP ์๋ต body์ ์ง์ ์ฐ๊ธฐ์ํด ์ฌ์ฉ๋จ.
@GetMapping("/request") @ResponseBody public String postmanTest(){ return "asd"; }
@RestController : @ResponseBody์ @Controller๊ฐ ๊ฒฐํฉ๋ ํํ, RESTful ์น ์๋น์ค ์ปจํธ๋กค๋ฌ ํด๋์ค๋ฅผ ์ ์ํจ. ๋ฉ์๋๊ฐ ๋ฐํํ๋ ๋ฐ์ดํฐ๋ฅผ HTTP body์ ์ฐ๊ธฐ ์ํด ์ฌ์ฉ๋๋ค. ๋ฐ์ดํฐ๋ฅผ View๋ก ์๋ตํ์ง ์๊ธฐ๋๋ฌธ์ API ๊ตฌํ์ ์ฌ์ฉํ๋ฉฐ, ๋ชจ๋ ๋ฉ์๋์ @ResponseBody๋ฅผ ์ ์ฉํ๋ค.
@RestController public class RestApiController { ... }
@Service : streotype ์ค ํ๋, ๋น์ฆ๋์ค ๋ก์ง์ ๋ด๋นํ๋ Service ํด๋์ค๋ฅผ ๋ํ๋. Bean ๋ฑ๋ก, DI, @Transactional ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ํธ๋์ญ์ ์ ๊ด๋ฆฌํ ์ ์๋ค.
@Service public class RestApiServiceLogic { ... }
@Repository : streotype ์ค ํ๋, ๋ฐ์ดํฐ ์ก์ธ์ค ํด๋์ค๋ก ์ฌ์ฉ๋๋ฉฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ์์ฉ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. DataAccessException ๋ฅผ ์ฌ์ฉํ์ฌ ์์ธ์ฒ๋ฆฌ ๋ฐ ๋ก๊น ์ ๋ณด๋ค ์ฝ๊ฒ ํ ์ ์๋ค.
@Repository public class RestApiRepositoryLogic{ ... }
@Slf4j : Logger ๊ฐ์ฒด ์์ฑ ์์ด ๋ฐ๋ก log.debug()๋ก ๋ก๊ทธ๋ฅผ ์ฐ์ด๋ณผ ์ ์๋ค.
@Slf4j public class apiTest{ public void testMethod(String msg){ log.debug("msg : " + msg); } }
AOP์์ ์ฌ์ฉ๋๋ Annotation
@Aspect : AOP ํด๋์ค๋ฅผ ์ ์ํ๋ ์ด๋ ธํ ์ด์ @Aspect class A{ }
@Before : ๋ฉ์๋๋ฅผ ์คํํ๊ธฐ ์ด์ ์ ์๋๋ ํจ์๋ก ์ง์ ํ๋ค.
@Before("b()") public void test2(){ }
@After : ๋ฉ์๋๋ฅผ ์คํํ ์ดํ์ ์๋๋ ํจ์๋ก ์ง์ ํ๋ค.
@After Returning("b()") public void test3(){ }
@Around : ํ๊ฒ ๋ฉ์๋ ์คํ ์ ํ๋ก ์ ์ฉ๋ ํจ์๋ฅผ ์ง์ ํ๋ค.
@Around("b()") public void test(){ }
@Pointcut : AOP๋ฅผ ์ ์ฉ์ํฌ ์ง์ ์ ์ค์ ํด์ฃผ๋ ํจ์
@Pointcut("execution(* com.test)") public void serviceMethods() { }
@AfterReturning : ๋ฉ์๋ ํธ์ถ ์ฑ๊ณต์ ์ ์ฉ๋ ํจ์๋ฅผ ์ง์ ํ๋ค.
@After Returning("b()") public void test3(){ }
@AfterThrowing : ๋ฉ์๋ ํธ์ถ์ ์์ธ๋ฐ์์ ์คํํ ํจ์๋ฅผ ์ง์ ํ๋ค.
@After Throwing("b()") public void test4(){ }
Lombok์ ๋ฐ๋ณต๋๋ Getter, Setter, ์์ฑ์, toString, builder ๋ฑ ์ฌ๋ฌ ๋ฉ์๋๋ฅผ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ฒ๋ฆฌํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค.
์ฃผ๋ก Model ํด๋์ค์์ ์ฌ์ฉํ๊ฒ ๋๋๋ฐ, ํด๋น ์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ฉด Getter, Setter์ ๊ฐ์ ๋ฉ์๋๊ฐ ์๋์์ฑ๋๋ค.
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int userNo;
private String userId;
private String userPw;
}
@Getter : ํ๋ ๋ณ์๋ค์ Getter๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ค.
@Setter : ํ๋ ๋ณ์๋ค์ Setter๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ค.
@AllArgsConstructor : ๋ชจ๋ ํ๋ ๋ณ์๋ค์ด ํฌํจ๋ ์์ฑ์๋ฅผ ์์ฑํ๋ค.
@NoArgsConstructor : ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์๋์ผ๋ก ์์ฑํ๋ค.
@Builder : Builder ํจํด์ ์ฌ์ฉํ๊ธฐ ์ํ ์ด๋
ธํ
์ด์
. ๋ฉ์๋์ฒด์ด๋์ ์ฌ์ฉํ ๊ฐ๋
์ฑ ์๋ ๊ฐ์ฒด ์์ฑ๊ฐ๋ฅ
-โป @AllArgsConstructor์ ์ถฉ๋์ด ๋ฐ์ํ ์ ์๊ธฐ๋๋ฌธ์ ์ฌ์ฉ์ ๊ธฐ๋ณธ์์ฑ์๋ฅผ ์์ฑํ๊ฑฐ๋ ํด๋น ์ด๋
ธํ
์ด์
์ ์ฌ์ฉํ์ง ๋ง์์ผํ๋ค.
์ด์ฒ๋ผ ํ๋์ ๋ฉ์๋๋ฅผ ์์ฑํ์ง ์๋๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ธํด Getter, Setter๋ฑ ์ด๋ ธํ ์ด์ ์ด ์๋ค๋ฉด ์๋์ผ๋ก ์์ฑ๋๋ค.