Spring

IoC Container - ResourceLoader Application에 포함한 리소스를 불러오는 기능을 제공하는 인터페이스이다. 기본적으로 ResourceLoader를 주입받아 사용할 수 있으나, ApplicationContext로도 같은 기능을 사용할 수 있다. ApplicationContext가 ResourceLoader를 상속받아 구현했기 때문이다. ResourceLoader AppRunner를 작성하여 활용법을 알아본다. Spring boot 프로젝트를 생성하고 아래의 소스코드를 작성한다. AppRunner @Component public class AppRunner implements ApplicationRunner { @Autowired ResourceLoader resourceLoad..
IoC Container - ApplicationEventPublisher 옵저버 패턴의 구현체로, 이벤트 기반 프로그래밍에 유용한 인터페이스를 제공한다. Observer pattern 옵저버 패턴엔 행위자, 관찰자가 존재한다. 행위자의 어떤 행동으로 변경 사항이 발생하면 즉시 관찰자에게 알려주는 것이 요점이다. 가장 대표적인 응용 사례는 데이터 바인딩이라고 할 수 있다. 웹 프론트엔드 개발에 가장 많이 사용되는 프레임워크 중 Angular, React 등은 데이터 바인딩을 지원하고 있다. View가 어떤 데이터를 바라보고 있고, 그 데이터가 변하는 즉시 View도 그 변화를 반영한다. 물론 View가 바뀐 데이터를 반영하는 과정엔 프로그래머의 개입이 필요하지 않다. Event Publisher / Su..
IoC Container - MessageSource Application을 다국화하는 방법을 제공하는 인터페이스 ApplicationContext는 MessageSource 인터페이스를 상속하고 있다. Spring boot를 사용한다면 기본적으로 messages.properties 를 활용할 수 있다. 파일 네이밍 규칙에 따라 자동으로 언어를 교환한다. 한국어 : messages_ko.properties 영어 : messages_en.properties Example messageSource를 사용하기에 앞서 default를 먼저 살펴보도록 한다. AppRunner @Component public class AppRunner implements ApplicationRunner { @Override pu..
IoC Container - Spring Environment Environment는 Profile, Property를 다루는 Interface이다. Profile Bean의 그룹 Spring은 다양한 profile을 정의할 수 있으며 사용하는 profile에 따라 다른 bean을 불러와 사용할 수 있다. @Profile("profile_name") 으로 지정한다. Profile은 단순하게 이름만으로도 사용할 수 있지만 기술 문서에는 아래와 같은 옵션도 지원한다고 기술되어 있다. ! : A logical "not" of the profile & : A logical "and" of the profiles | : A logical "or" of the profiles @Profile("!dev") 인 경우..
IoC Container - Bean의 Scope Scope type Singleton (Default) App에서 1개의 인스턴스만 존재 Proto 매번 다른 인스턴스 생성 Singleton과 Proto를 만들어서 눈으로 확인해보자. 의존성 없이 사용할 땐 별다른 문제가 없지만, Singleton과 Proto 사이 의존 관계가 있다면 의도대로 작동하지 않을 수 있다. Example 1 Singleton @Component class Singleton { } Prototype @Component @Scope(“prototype”) class Prototype { } AppRunner (결과 확인용) @Component public class AppRunner implements ApplicationRun..
Bean 생성 추적해보기 Spring core를 공부해보겠답시고 빌드한 뒤 소스코드를 두서없이 읽고만 있으려니, 진행이 안 되는 느낌이 들었다. 여긴 뭐고 저긴 또 뭔지... 그래서 실제로 앱을 실행한 뒤 추적하는 방법을 사용해보기로 하였다. core에 대해서 공부를 한 덕분에 추적 경로를 만들어서 디버깅을 할 수 있었다. 먼저, Spring boot 2 이상 버전을 사용하여 프로젝트를 생성해보자. @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }main은 위의 코드처럼 생성되었을 것이다..
IoC Container - ApplicationContext 역할 Bean instance 생성 의존 관계 설정 Bean 제공 Bean 등록 방법 XML 으로 등록 모든 bean을 수동으로 등록해야 하기 때문에 번거로움 구현체 : ClassPathXmlApplicationContext Java @Configuration annotation을 붙인 class 사용 @Bean으로 직접 선언 구현체 : AnnotationConfigApplicationContext Component-scan Spring 2.5부터 지원 @Component, @Service, @Control 등 Bean 등록에 사용되는 Annotation을 scan하여 자동으로 등록 @Autowired IoC container에서 Bean을 ..
https://github.com/spring-projects/spring-framework/wiki/Build-from-Source spring-projects/spring-framework Spring Framework. Contribute to spring-projects/spring-framework development by creating an account on GitHub. github.com 위 가이드를 이용해서 소스코드를 빌드했다. 차례대로 따라가면 문제 없이 소스코드를 불러올 수 있다. Dependency List 1. Git 2. JDK 8 Update 60 ↑ Windows인 경우엔 power shell, 리눅스나 MAC인 경우 터미널을 켜고 작업하면 된다. 먼저, 소스코드를 Gi..
감동이중요해
'Spring' 태그의 글 목록 (2 Page)