๐ Spring
์คํ ํ๊ฒฝ Spring Boot 2.7.1 Gradle 7.4.1 Maven 3.8.5 ๋น๋ ์์คํ
์ค์ Gradle ๋๋ณด๊ธฐ plugins { id 'org.springframework.boot' version '2.7.1' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' id 'org.asciidoctor.jvm.convert' version '3.3.2' // (1) } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '11' configurations { compileOnly { extendsFrom annotationProcessor } }..
๐ Spring
ํ์ ๋๋ฉ์ธ์์ Username์ด ์ค๋ณต์ด ๋๋ฉด ์๋๋ฏ๋ก ์๋ ๊ฐ์ด ๋ก์ง์ ์ถ๊ฐํ์ต๋๋ค. Optional account = accountRepository.findByUsername(username); if (account.isPresent()) throw new AccountDuplicateException(username); ํ์ง๋ง Optional์ ๋ฐํํ์ง ๋ง๊ณ ifPresent๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ๋ก Exception์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. accountRepository.findByUsername(username) .ifPresent(a -> { throw new AccountDuplicateException(username) }); ํจ์ฌ ๋ ๊ฐ๊ฒฐํด์ง๊ณ ์์๋ณด๊ธฐ ์ฌ์ด ์ฝ๋๊ฐ ๋์์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค.
๐ Spring/Spring Security
Spring boot + Security ๋ฅผ ์ฌ์ฉํ๊ณ ์๋๋ฐ ์๋ฌ ์ฝ๋ ๋ง๋ค ํ์ด์ง๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์ถ์๋ฐ ์ด๋ป๊ฒ ํด์ผํ๋์ง ๋ฐฉ๋ฒ์ ์๊ฐํด๋ณด๋ค๊ฐ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๊ฐ๋ฌ์ต๋๋ค. 1. EntryPoint, Handler ์ฌ์ฉ authenticationEntryPoint, accessDeniedHandler์์ ์ฌ์ฉ์์๊ฒ ํด๋น ์๋ฌ ํ์ด์ง๋ก ๋ฆฌ๋ค์ด๋ ์
์ ์ํค๋ ๋ฐฉ๋ฒ์
๋๋ค. Security Config /* Security Config */ @Override protected void configure(HttpSecurity http) throws Exception { http // .. .exceptionHandling() .authenticationEntryPoint(new MyAuthenticationEntryP..
๐ Spring
Mockito๋ก ํ
์คํธ๋ฅผ ์์ฑํด๋ณด์ ํ๋ก์ ํธ ํ๊ฒฝ: Spring-boot, Junit5, Mockito ๋ชจ๋ ์ฝ๋๋ Github์ ์์ต๋๋ค :) Mockito Mockito๋? ํ
์คํธ ํ๋ ์์ํฌ๋ก ํ
์คํธํ ๋ ๋ชจ๋ ๋น์ ์ผ์ผํ ์ฃผ์
์ํค์ง ์๊ณ , Mock ๊ฐ์ฒด(๊ฐ์ง ๊ฐ์ฒด)๋ฅผ ์ฃผ์
์์ผ ํ์๋ฅผ ํ
์คํธํ ์ ์๊ณ , ์์ง ์์ฑ๋์ง ์์ ์ฝ๋๋ค(์์กด์ฑ ๊ฐ์ฒด) ๋๋ ๊ตฌํํ๊ธฐ ์ด๋ ค์ด ์์กด์ฑ ๊ฐ์ฒด๋ค์ Mockingํ์ฌ ํ
์คํธ ํ ์ ์์ต๋๋ค. ์์กด์ฑ (dependency) spring boot๋ฅผ ์ฌ์ฉ์ค์ด๋ผ๋ฉด, spring-boot-starter-test ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ด๋ฏธ JUnit5์ Mockito๊ฐ ํฌํจ๋์ด์์ผ๋ฏ๋ก ์์กด์ฑ์ ์ถ๊ฐํด์ฃผ์ง ์์๋ ๋ฉ๋๋ค. Spring.io - 41.Testing Mock ํ
์คํธ ์์ moc..
๐ Spring
Spring H2 inmemory DB ์ฌ์ฉํ๋ ๋ฒ Spring boot์์ h2 ์ธ๋ฉ๋ชจ๋ฆฌ DB ์ฌ์ฉํ๋ ๋ฒ์ ์์๋ณด์. ์์กด์ฑ ์ถ๊ฐ com.h2database h2 runtime application.yml ์ค์ spring: h2: console: enabled: true path: /h2-console #(default ์ค์ ์ํด๋ ์๋์ ์ฉ) datasource: url: jdbc:h2:mem:todoItem ์ด์ http://localhost:8080/h2-console ๋ก ์ ์ํ๊ฒ ๋๋ฉด, h2 ์ฝ์์ ๋ณผ ์ ์๋ค. saved setting: Generic H2 (Embedded) JDBC URL: jdbc:h2:mem:todoItem ๋น์ฐํ ์ธ๋ฉ๋ชจ๋ฆฌ ์ด๊ธฐ ๋๋ฌธ์ ํ๋ก์ ํธ๊ฐ ์ข
๋ฃ๋๋ฉด, ๋ด๋ถ ๋ฐ์ดํฐ๋..
๐ Spring/Spring in Action
Goal ์คํ๋ง JdbcTemplate ์ฌ์ฉํด๋ณด๊ธฐ SimpleJdbcInsert๋ฅผ ์ฌ์ฉํด์ ๋ฐ์ดํฐ ์ถ๊ฐํด๋ณด๊ธฐ ์คํ๋ง ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํด์ JPA ์ ์ธํ๊ณ ์ฌ์ฉํด๋ณด๊ธฐ ์์ JDBC Jdbc๋, Java DataBase Connectivity, ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฐ๊ฒฐํ๊ธฐ ์ํ API์ด๋ค. Jdbc๋ฅผ ์ด์ฉํด select ์ฟผ๋ฆฌ ์์ฑํด๋ณด๋ฉด ์๋์ ๊ฐ๋ค. @Override public Account findById(String id) { Connection conn = null; PreparedStatement st = null; ResultSet rs = null; String sql = "select id, name from Account where id = ?"; try { conn = datasource.getCo..