Servlet์์ ์คํ๋ง ApplicationContext ์ฌ์ฉํ๊ธฐ Servlet์์ ์คํ๋ง ApplicationContext ์ฌ์ฉํ๊ธฐ pom.xml org.springframework spring-webmvc 5.1.3.RELEASE ์คํ๋ง ApplicationContext, ์ฆ ์คํ๋ง IoC๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๊ด๋ จ ์์กด์ฑ์ ์ถ๊ฐํด์ผ ํ๋ค. spring-context ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ์์กด์ฑ์ ์ถ๊ฐํ๋ค. ์ ์์ ์์๋ spring-webmvc๋ฅผ ๊ฐ์ ธ์ค๋๋ก ํ์ง๋ง spring-context๋ง ๊ฐ์ ธ์๋ ๋๋ค. web.xml org.springframework.web.context.ContextLoaderListener contextClass org.springframework.web.context.support.AnnotationConfigWebApplication.. 2020. 3. 11. [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] EnvironmentCapable - Profile ์ฌ์ฉํ๊ธฐ [Spring] EnvironmentCapable - Profile ์ฌ์ฉํ๊ธฐ Spring์ ApplicationContext๋ BeanFactory ๊ธฐ๋ฅ๋ง ํ๋๊ฑด ์๋๋ค. ApplicationContext๊ฐ ์์๋ฐ๋ ๋ค์ํ ์ธํฐํ์ด์ค๋ค ์ค EnvironmentCapable ์ธํฐํ์ด์ค๋ 'Profile(ํ๋กํ์ผ)' ์ด๋ผ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. Profile(ํ๋กํ์ผ)์ ๊ฐ๋ ํ๋กํ์ผ = ๋น๋ค์ ๋ฌถ์ ํ๋กํ์ผ์ ํน์ ์คํ ํ๊ฒฝ์์ ์ฌ์ฉํ ๋น๋ค์ ๋ฌถ์์ด๋ค. ํ ์คํธ ํ๊ฒฝ์์ ์ฌ์ฉํ ๋น ๋ฌถ์๊ณผ ํ๋ก๋์ (์ด์) ํ๊ฒฝ์์ ์ฌ์ฉํ ๋น ๋ฌถ์์ด ์๋ก ๋ค๋ฅผ ์ ์๋ค. ๋ ๊ฐ ํ๊ฒฝ์ ๋ฐ๋ผ ์๋ก ๋ค๋ฅธ ๋น๋ค์ ์จ์ผํ๋ ๊ฒฝ์ฐ, ํน์ ํ๊ฒฝ์์๋ง ๋ฑ๋กํด์ผํ๋ ๋น๋ค์ด ์๋ ๊ฒฝ์ฐ๊ฐ ์์ ์ ์๋ค. ํ๋กํ์ผ์ ๊ทธ๋ฌํ ์๊ตฌ์ฌํญ์ ์ถฉ์กฑํ ์ ์๋.. 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] @Autowired์ ๋ค์ํ ์ฌ์ฉ ๋ฐฉ๋ฒ - required, Primary, Qualifier [Spring] @Autowired์ ๋ค์ํ ์ฌ์ฉ ๋ฐฉ๋ฒ - required, Primary, Qualifier 1. ์์กด๊ฐ์ฒด ํ์ ์ ๋น์ด ์๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด BookService ํด๋์ค์ BookRepository ์ธํฐํ์ด์ค๊ฐ ์๋ค. import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class BookService { BookRepository bookRepository; @Autowired public BookService(BookRepository bookRepository) { this.bookRepository = boo.. 2020. 3. 1. [Spring] ๋น์ ์ค์ ํ๋ 3๊ฐ์ง ๋ฐฉ๋ฒ - XML, JAVA, Component Scan [Spring] ๋น์ ์ค์ ํ๋ 3๊ฐ์ง ๋ฐฉ๋ฒ - XML, JAVA, Component Scan ์ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ค. ์์ ์ฝ๋์ ํ์ํ ์์กด์ฑ์ ๋ฐ๊ธฐ ์ํด spring boot ํ๋ก์ ํธ๋ฅผ ์์ฑํ ๊ฒ์ด๋ค. ์ฐธ๊ณ ๋ก ์์ ์ฝ๋์๋ ์ด ์์กด์ฑ์ด ํ์ํ๋ค. Spring Initializr๋ฅผ ์ ํํ๊ณ Next๋ฅผ ํด๋ฆญํ๋ค. Group, Artifact, Name์ ์ ๋ ฅํ๊ณ Next๋ฅผ ํด๋ฆญํ๋ค. Group์ ํจํค์ง๋ช ์ ์ ์ด์ค๋ค. (๋ณดํต ๋๋ฉ์ธ๋ช ์ ๋ค์ง์ด์..) Web - Spring Web์ ๋ฃ๊ณ Next๋ฅผ ํด๋ฆญํ๋ค. Finish๋ฅผ ํด๋ฆญํด์ ์ ๊ท ํ๋ก์ ํธ ์์ฑ์ ์๋ฃํ๋ค. Spring boot ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์๋ค. pom.xml์ ์ด์ด๋ณด๋ฉด spring boot 2.2.5 ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ก์์ ํ์ธํ ์ ์๋ค. ์ฐ์ธก์.. 2020. 3. 1. [Spring] ์ ์๋ฐ ๊ฐ์ฒด๋ฅผ IoC ์ปจํ ์ด๋์ ๋น์ผ๋ก ๋ง๋ค๊น? [Spring] ์ ์๋ฐ ๊ฐ์ฒด๋ฅผ IoC ์ปจํ ์ด๋์ ๋น์ผ๋ก ๋ง๋ค๊น? IoC๋ spring์ ํตํด์๋ง ์ ์ฉํ ์ ์๋๊ฒ ์๋๋ฉฐ ์ผ๋ฐ ์๋ฐ ๊ฐ์ฒด๋ก DI๋ฅผ ์ง์ ๊ตฌํํ ์๋ ์๋ค. ๊ทธ๋ฐ๋ฐ๋ spring์ IoC ์ปจํ ์ด๋๋ฅผ ์ฌ์ฉํ๋ ์ด์ ๋ ๋ค์๊ณผ ๊ฐ๋ค. DI(Dependency Injection, ์์กด์ฑ ์ฃผ์ ) DI๋ฅผ ์ง์ ๊ตฌํํ๋ ๊ฒ ๋ณด๋ค spring์์ ์ ๊ณตํ๋ IoC ์ปจํ ์ด๋์ DI๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด ํธ๋ฆฌํ๊ณ ํจ์จ์ ์ด๋ค. ๊ฐ์ฒด์ Scope ์๋ฐ ๊ฐ์ฒด๋ฅผ IoC ์ปจํ ์ด๋์ ๋น์ผ๋ก ๋ง๋ค๋ฉด ๊ฐ์ฒด์ scope๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ฉ์ดํ๋ค. IoC ์ปจํ ์ด๋์์ ๊ด๋ฆฌ๋๋ ๋น๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฑ๊ธํค scope๋ก ๋ฑ๋ก๋๋ค. ๋ฐ๋ผ์ ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ฐ์ ์ผ๋ก ์ค๋ก์ง ํ๋์ ์ธ์คํด์ค๋ง ์ฌ์ฉํด๋ ๋๋ ๊ฐ์ฒด๋ผ๋ฉด IoC ์ปจํ ์ด๋์ ๋น์ผ๋ก ๋ฑ๋กํด์ ์.. 2020. 3. 1. ์ด์ 1 ๋ค์