[Spring] SpEL - Spring Expression Language [Spring] SpEL - Spring Expression Language SpEL(Spring Expression Language)๋? Spring expression language๋ ๋ณดํต SpEL๋ก ํ๊ธฐํ๋ฉฐ ๊ตฌ๋๋ก๋ ์คํ๋ง EL์ด๋ผ๊ณ ์ง์นญํ๋ค. SpEL์ ๋ณดํต ๊ฐ์ฒด๋ฅผ ์กฐํํ๊ณ ์กฐ์ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ฌ Unified EL๊ณผ ์ ์ฌํ์ง๋ง ๋ฉ์๋ ํธ์ถ, ๋ฌธ์์ด ํ ํ๋ฆฟ ๊ธฐ๋ฅ ๋ฑ์ ์ฌ๋ฌ๊ฐ์ง ์ถ๊ฐ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ํํ์ ์ธ์ด์ด๋ค. OGNL, MVEL, JBoss EL ๋ฑ ์๋ฐ์์ ์ฌ์ฉ๊ฐ๋ฅํ ์ฌ๋ฌ EL์ด ์์ง๋ง SpEL์ Spring ํ๋ก์ ํธ ์ ๋ฐ์ ๊ฑธ์ณ ์ฌ์ฉํ๊ธฐ ์ํด ๋ง๋ค์ด์ก์ผ๋ฉฐ ์คํ๋ง 3.0๋ถํฐ ์ง์๋๋ค. SpEL ํ๊ธฐ๋ฒ #{ SpELํํ์ } JSP์์ EL์ ์ฌ์ฉํด์ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ๊บผ๋ผ๋ ${ } ์ด.. 2020. 3. 7. [Spring] ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ - PropertyEditor, Converter ๊ทธ๋ฆฌ๊ณ Formatter [Spring] ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ - PropertyEditor, Converter ๊ทธ๋ฆฌ๊ณ Formatter 1. Data Binding(๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ)์ ์ ์ ๐ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ์น์์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ์ฌ ์ ๋ฌ๋๋ ๊ฐ์ ์ฃผ๋ก ๋ฌธ์์ด์ด๋ฉฐ, ์น ์ดํ๋ฆฌ์ผ์ด์ ์์๋ ์ด ๋ฌธ์์ด์ ๋๋ฉ์ธ ๊ฐ์ฒด์ ํ๋กํผํฐ ํ์ (int, date, boolean, ... ๋๋ ๋๋ฉ์ธ ๊ฐ์ฒด ํ์ ๊ทธ ์์ฒด)์ผ๋ก ๋ณํ์ด ํ์ํ๋ค. ์ด๋ ๊ฒ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋ฌธ์์ด ๊ฐ์ ํ๋กํผํฐ ํ์ ์ ๋ง์ถฐ ๋ณํํ์ฌ ํ ๋นํ๋ ๊ฒ์ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ด๋ผ ํ๋ค. 2. Spring์ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ Spring์ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ๊ธฐ๋ฅ์ ์ฌ๋ฌ ์ธํฐํ์ด์ค๋ก ์ถ์ํํ์ฌ ์ ๊ณตํ๋ค. ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ์ธํฐํ์ด์ค๋ ์ฃผ๋ก ์น MVC์์ ์ฌ์ฉํ์ง๋ง web์ ๊ตญํ๋์ด ํนํ๋ ๊ธฐ๋ฅ์ด ์๋๋ผ ์ฌ๋ฌ ๊ณณ.. 2020. 3. 6. [Spring] Validation ์ถ์ํ [Spring] Validation ์ถ์ํ org.springframework.validation.Validator๋ spring์์ ์ ๊ณตํ๋ ๊ฐ์ฒด ๊ฒ์ฆ์ฉ ์ธํฐํ์ด์ค์ด๋ค. ์ฃผ๋ก spring mvc์์ ์ฌ์ฉํ์ง๋ง web ๊ณ์ธต ์ ์ฉ ๊ฐ๋ ์ ์๋๋ฉฐ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋ ์ ์๋ค. Validator๋ Java EE ํ์ค ์คํ ์ค ํ๋์ธ Bean Validation์ ์ง์ํ์ฌ Bean Validation์ด ์ ๊ณตํ๋ ์ฌ๋ฌ ์ ๋ ธํ ์ด์ ๋ค์ ์ฌ์ฉํด์ ๊ฐ์ฒด ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ฆํ ์ ์๋ค. 1. Validator ์ธํฐํ์ด์ค Validator ์ธํฐํ์ด์ค์๋ ๋ ๊ฐ์ง ๋ฉ์๋๊ฐ ์ ์๋์ด์๋ค. 1 2 3 4 5 6 boolean supports(Class clazz) { // ์ธ์คํด์ค๊ฐ ๊ฒ์ฆ ๋์ ํ์ ์ธ์ง ํ์ธ } void validate.. 2020. 3. 5. [Spring] Resource ์ถ์ํ [Spring] Resource ์ถ์ํ Spring์ java.net.URL์ org.springframework.core.io.Resource๋ก ๊ฐ์ธ์ ์ถ์ํํ ํด๋์ค๋ฅผ ์ ๊ณตํ๋ค. ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ๋ ๋ฆฌ์์ค ์์ฒด๋ฅผ ์ถ์ํ ํ๊ฒ์ธ๋ฐ spring์์ ์ด๋ฅผ ์ถ์ํํ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค. java.net.URL์ http, https, ftp๋ ์ง์ํ์ง๋ง classpath๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฆฌ์์ค๋ฅผ ๊ฐ์ ธ์ค๋ ๊ธฐ๋ฅ์ด ์๋ค. ServletContext๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋ ๊ฒฝ๋ก๋ก ๋ฆฌ์์ค๋ฅผ ๊ฐ์ ธ์ค๋ ๊ธฐ๋ฅ์ด ์๋ค. ์๋ก์ด ํธ๋ค๋ฌ๋ฅผ ๋ฑ๋กํ์ฌ URL ์ ๋ฏธ์ฌ๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ ์๋ ์์ง๋ง ๊ตฌํ์ด ๋ณต์กํ๊ณ ํธ์์ฑ ๋ฉ์๋๊ฐ ๋ถ์กฑํ๋ค. Spring์ ๋ฆฌ์์ค๋ฅผ ์ถ์ํํ์ฌ ๋ฆฌ์์ค๋ฅผ ๋ค๋ฃจ๋๋ฐ ์์ด ํ๋์ ํต์ผ๋ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค. 1. S.. 2020. 3. 5. [Spring] ResourceLoader๋ก ํ ์คํธ ํ์ผ ์ถ๋ ฅํ๊ธฐ (Java 11) [Spring] ResourceLoader๋ก ํ ์คํธ ํ์ผ ์ถ๋ ฅํ๊ธฐ (Java 11) Spring์ ApplicationContext๋ ResourceLoader๋ผ๋ ์ธํฐํ์ด์ค๋ฅผ ์์ํ๋ค. ์ด ์ธํฐํ์ด์ค๋ ์ด๋ฆ์์๋ถํฐ ์ ์ ์๋ฏ ๋ฆฌ์์ค๋ฅผ ์ฝ์ด์ค๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. 1. Resource ๊ฐ์ฒด ์ป๊ธฐ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.sprin.. 2020. 3. 5. [Spring] Spring ์ดํ๋ฆฌ์ผ์ด์ ๋ฉ์์ง ๋ค๊ตญ์ด ๋ฐ ์ฌ๋ก๋ฉ ์ฒ๋ฆฌ [Spring] Spring ์ดํ๋ฆฌ์ผ์ด์ ๋ฉ์์ง ๋ค๊ตญ์ด ๋ฐ ์ฌ๋ก๋ฉ ์ฒ๋ฆฌ Spring IoC ์ปจํ ์ด๋ = ApplicationContext๊ฐ ๊ฐ๊ณ ์๋ ๊ธฐ๋ฅ ์ค MessageSource๋ ๊ตญ์ ํ(i18n)์ ๊ด๋ จ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํด์ฃผ๋๋ฐ ์ด๋ฅผ ์ด์ฉํด์ ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ถ๋ ฅํ๋ ๋ฉ์์ง๋ฅผ ๋ค๊ตญํํ ์ ์๋ค. 1. ๋ฉ์์ง ํ์ผ ์์ฑ resources ๋๋ ํ ๋ฆฌ ์๋์ messages.properties, messages_ko_KR.properties ํ์ผ์ ์๋ก ์์ฑํ๋ค. ์ฌ๊ธฐ์ ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ถ๋ ฅํ ๋ฉ์์ง๋ฅผ messages.properties์๋ ์์ด๋ก, messages_ko_KR.proeprties์๋ ํ๊ตญ์ด๋ก ์์ฑํ ๊ฒ์ด๋ค. ํ์ผ๋ช ์ ์คํ๊ฐ ๋์ง ์๋๋ก ์กฐ์ฌํ์. ํ์ผ์ ์์ฑํ๊ณ ๋๋ฉด ์๋์ผ๋ก 'messages.. 2020. 3. 4. [Spring] EnvironmentCapable - Property ์ฌ์ฉํ๊ธฐ ๊ด๋ จ ๊ธ - JAVA ์น ํ๋ก์ ํธ์์ Properties ํ์ผ ํ์ฉํ๊ธฐ - [Spring] EnvironmentCapable - Profile ์ฌ์ฉํ๊ธฐ [Spring] EnvironmentCapable - Property ์ฌ์ฉํ๊ธฐ ApplicationContext๋ EnvironmentCapable์ ์์๋ฐ์ผ๋ฉฐ, EnvironmentCapable์ด ์ ๊ณตํ๋ ์ฃผ์ ๊ธฐ๋ฅ์๋ profile๊ณผ property๊ฐ ์๋ค. ๋ณธ ํฌ์คํ ์์๋ ๊ทธ ์ค property์ ๋ํด ์ดํด๋ณธ๋ค. 1. Property ๊ธฐ๋ฅ ์ดํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉ๋๋ ์ฌ๋ฌ๊ฐ์ง key, value ์์ผ๋ก ์ ๊ณต๋๋ ํ๋กํผํฐ์ ๊ฐํธํ๊ฒ ์ ๊ทผํ ์ ์๋ ๊ธฐ๋ฅ์ด๋ค. Spring์ ํ๋กํผํฐ์ '๊ณ์ธตํ'์ผ๋ก ์ ๊ทผํ๋๋ฐ ๊ณ์ธตํ์ผ๋ก ์ ๊ทผํ๋ค๋ ๋ง์ ํ๋กํผํฐ์ ์ฐ์ ์์๊ฐ.. 2020. 3. 4. [Spring] ๋น์ Scope - ์ฑ๊ธํค๊ณผ ํ๋กํ ํ์ [Spring] ๋น์ Scope - ์ฑ๊ธํค๊ณผ ํ๋กํ ํ์ ๋น์ ๋ฑ๋กํ ๋ ์๋ฌด๋ฐ ์ค์ ์ ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ๋น์ ์ฑ๊ธํค scope์ ๊ฐ๋๋ค. ์ฑ๊ธํค scope์ด๋ ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ฐ์ ๊ฑธ์ณ ํด๋น ๋น์ ์ธ์คํด์ค๋ฅผ ์ค์ง ํ๋๋ง ์์ฑํด์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. 1. Singleton Scope Single, Proto ํด๋์ค๋ฅผ ์๋ก ๋ง๋ค๊ณ @Component๋ฅผ ๋ถ์ฌ ๋น์ผ๋ก ๋ฑ๋กํ๋ค. Single.java 1 2 3 4 5 6 7 8 9 10 11 12 13 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class Single { @.. 2020. 3. 3. [Spring] Component Scan๊ณผ Function์ ์ฌ์ฉํ ๋น ๋ฑ๋ก ๋ฐฉ๋ฒ [Spring] Component Scan๊ณผ Function์ ์ฌ์ฉํ ๋น ๋ฑ๋ก ๋ฐฉ๋ฒ 1. @ComponentScan @ComponentScan ์ ๋ ธํ ์ด์ ์ spring 3.1๋ถํฐ ๋์ ๋์ผ๋ฉฐ ์ค์ ๋ ์์ ์ง์ ๋ถํฐ ์ปดํฌ๋ํธ ํด๋์ค๋ฅผ scanningํ์ฌ ๋น์ผ๋ก ๋ฑ๋กํด์ฃผ๋ ์ญํ ์ ํ๋ค. ์ปดํฌ๋ํธ ํด๋์ค๋ ๋ค์ ์ ๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ฅผ ์๋ฏธํ๋ค. @Component @Repository @Service @Controller @Configuration @ComponentScan์ ๊ฐ์ฅ ์ค์ํ ๋ ๊ฐ์ง ์์ฑ์ component๋ฅผ scanํ ์์ ์ง์ ์ ์ค์ ํ๋ ์์ฑ๊ณผ scanํ component ์ค ๋น์ผ๋ก ๋ฑ๋กํ์ง ์์ ํด๋์ค๋ฅผ ์ ์ธํ๋ ํํฐ ์์ฑ์ด๋ค. 1) Scan ์์ ์ง์ ์ค์ - basePackages()์.. 2020. 3. 2. [Spring] ์คํ๋ง AOP ๊ฐ๋ ์ดํด ๋ฐ ์ ์ฉ ๋ฐฉ๋ฒ [Spring] ์คํ๋ง AOP ๊ฐ๋ ์ดํด ๋ฐ ์ ์ฉ ๋ฐฉ๋ฒ 1. AOP(Aspect Oriented Programming) Spring์ Spring Triangle์ด๋ผ๊ณ ๋ถ๋ฅด๋ ์ธ ๊ฐ์ง ๊ฐ๋ ์ ์ ๊ณตํด์ค๋ค. ๊ฐ๊ฐ IoC, AOP, PSA๋ฅผ ์ผ์ปซ๋๋ค. AOP๋ Aspect Oriented Programming์ ์ฝ์๋ก '์ธก๋ฉด/์์ ์งํฅ์ ์ธ ํ๋ก๊ทธ๋๋ฐ'์ด๋ผ๋ ์๋ฏธ์ด๋ค. '์ธก๋ฉด/์์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ'์ด ๋ฌด์์ ์๋ฏธํ๋๊ฐ? class A { method a() { AAAA method a๊ฐ ํ๋ ์ผ๋ค BBBB } method b() { AAAA method b๊ฐ ํ๋ ์ผ๋ค BBBB } } class B { method c() { AAAA method c๊ฐ ํ๋ ์ผ๋ค BBBB } } cs ์์ ๊ฐ์ด ๋์ผํ ์ผ์ ํ๋.. 2020. 2. 29. [Spring] ์์กด์ฑ ์ฃผ์ (DI, Dependency Injection)์ ์ธ๊ฐ์ง ๋ฐฉ๋ฒ [Spring] ์์กด์ฑ ์ฃผ์ (DI, Dependency Injection)์ ์ธ๊ฐ์ง ๋ฐฉ๋ฒ Spring์ @Autowired ์ ๋ ธํ ์ด์ ์ ์ด์ฉํ ๋ค์ํ ์์กด์ฑ ์ฃผ์ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค. @Autowired ์ ๋ ธํ ์ด์ ์ Spring์๊ฒ ์์กด์ฑ์ ์ฃผ์ ํ๋ผ๋ ์ง์์ ์ญํ ๋ก ์ฐ์ด๋๋ฐ ์์ฑ์, ํ๋, ์ธํฐ์ ๋ถ์ผ ์ ์๋ค. ๐จ Spring IoC ์ปจํ ์ด๋์ ์ํ ์์กด์ฑ ์ฃผ์ ์ ๋น(Bean) ๋ผ๋ฆฌ๋ง ๊ฐ๋ฅํ๋ค. 1. ์์ฑ์ ์ฃผ์ 1 2 3 4 5 6 7 8 9 @Component public class SampleController { private SampleRepository sampleRepository; @Autowired public SampleController(SampleRepository sampleRepo.. 2020. 2. 28. [Spring] ์คํ๋ง ๋น(Bean)์ ๊ฐ๋ ๊ณผ ์์ฑ ์๋ฆฌ [Spring] ์คํ๋ง ๋น(Bean)์ ๊ฐ๋ ๊ณผ ์์ฑ ์๋ฆฌ ๋น(Bean) Spring IoC ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด๋ฅผ ๋น(Bean)์ด๋ผ๋ ์ฉ์ด๋ก ๋ถ๋ฅธ๋ค. ์ฐ๋ฆฌ๊ฐ new ์ฐ์ฐ์๋ก ์ด๋ค ๊ฐ์ฒด๋ฅผ ์์ฑํ์ ๋ ๊ทธ ๊ฐ์ฒด๋ ๋น์ด ์๋๋ค. ApplicationContext.getBean()์ผ๋ก ์ป์ด์ง ์ ์๋ ๊ฐ์ฒด๋ ๋น์ด๋ค. ์ฆ Spring์์์ ๋น์ ApplicationContext๊ฐ ์๊ณ ์๋ ๊ฐ์ฒด, ์ฆ ApplicationContext๊ฐ ๋ง๋ค์ด์ ๊ทธ ์์ ๋ด๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค. ์ด๋ป๊ฒ Spring IoC ์ปจํ ์ด๋์ ๋น์ ๋ฑ๋กํ ๊น? ๋น์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ค์ํ์ง๋ง ๊ธฐ๋ณธ์ ์ผ๋ก ํฌ๊ฒ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ด ์๋ค. โ Component Scanning โก ๋น ์ค์ ํ์ผ์ ์ง์ ๋น์ ๋ฑ๋ก Component Scan @Comp.. 2020. 2. 28. ์ด์ 1 2 3 ๋ค์