springboot(7)
-
[Spring] Bean LifeCycle & Callback Method
📌 Life-Cycle 기본적인 SpringBean의 Life-Cycle은 다음과 같은 과정을 가진다. "객체 생성" => "의존관계 주입" SpringBean은 객체를 생성하고 의존관계 주입이 다 끝나야 필요한 데이터를 가져올 수 있는 준비가 완료된다. 따라서, 초기화 작업은 의존관계 주입이 다 끝난 후에 호출하여야 한다. ※ Constructor Injection은 유일하게 객체 생성과 같이 이뤄진다. # What is Dependency Injection? [Spring] 의존 관계 주입 (DI : Dependency Injection) 📌 DI(Dependency Injection)의 4가지 방법 1. 생성자 주입 (constructor injection) 2. 수정자 주입 (setter inje..
2024.03.21 -
[Spring] 의존 관계 주입 (DI : Dependency Injection)
📌 DI(Dependency Injection)의 4가지 방법 1. 생성자 주입 (constructor injection) 2. 수정자 주입 (setter injection) 3. 필드 주입 (field injection) 4. 일반 메서드 주입 (method injection) # Constructor Injection (권장 O) 생성자를 통해서 의존 관계를 주입받는 방식이다. 생성자 호출 시점에 딱 1번만 호출되는 것이 보장된다. "불변", "필수" 의존 관계에 유효하다. (Getter, Setter => X, 값이 무조건 존재) Bean이고 생성자가 한 개일 때, @Autowired를 생략해도 자동으로 의존 관계가 주입된다. @Service public class MemberServiceImpl ..
2024.03.17 -
[Spring] @ComponentScan & @Autowired
📌 @ComponentScan # What id it? 1. 직접적으로 설정 정보 및 SpringBean을 등록하지 않아도 자동으로 Bean을 등록하는 기능이다. 2. @Component가 붙은 클래스를 자동으로 스캔하여 Bean으로 등록한다. @Configuration도 내부에 @Component라는 어노테이션이 붙어있다. 3. Bean의 기본 이름은 클래스이름의 맨 앞 문자를 소문자로 하여 사용한다. ex) ClassName : MemberServiceImple => BeanName : memberServiceImpl # Starting Position 1. basePackages="" 속성으로 탐색을 시작할 Package의 위치를 지정할 수 있다. 2. basePackageClasses="" 속성으..
2024.03.16 -
[Spring] Singleton & Singleton Container
📌 Singleton # Why Use it? 1. WebApplication의 특징은 User(=사용자)가 수많은 요청을 보내는 구조이다. 2. 요청이 올때마다 새로운 객체가 생성된다. ex) 사용자 트래픽이 초당 100개가 요청되면 초당 100개의 객체 생성 및 소멸 >> 메모리 낭비가 심하다!! - 해결방안 : 객체를 1개만 생성하고 이 객체를 공유하도록 설계한다. >> Singleton Pattern # What is Singleton Pattern? - Class의 인스턴스가 딱 1개만 생성되는 것을 보장하는 디자인 패턴 # How to make? - private 접근 지정자를 사용하여 외부에서 임의로 new 키워드를 사용하지 못하게 한다. public class SingletonService..
2024.03.15 -
[Spring] BeanFactory와 ApplicationContext
📌 BeanFactory SpringContainer의 최상위 Interface이다. SpringBean을 관리하고 조회하는 기능을 가지고 있다. 📌 ApplicationContext BeanFactory를 상속받은 Interface이다. 상속 관계로 이루어져 있기에 BeanFactory가 가진 기본적인 기능을 모두 가지고 있다. 📌 BeanFactory vs ApplicationContext Application 개발 및 관리에는 수많은 부가기능을 필요로 한다. ApplicationContext는 BeanFactory를 제외하고도 MessageSource, EnvironmentCapable, ApplicationEventPublisher, ResourceLoader 등의 Interface를 상속받으며 ..
2024.03.14 -
[Spring] Spring 핵심 원리
📌 Spring Container 생성 // App ApplicationContext applicationContext = new AnnotationConfigApplicationContext(xxx.class); 1. ApplicationContext를 스프링 컨테이너라고 한다. 2. SpringContainer는 XML 방식, Annotation 방식의 2가지 방식으로 생성할 수 있다. 3. ApplicationContext는 Interface이다. 4. new AnnotationConfigApplicationContext()는 Interface의 구현 객체이고, 파라미터로 Class 파일을 받는다. 📌 Bean 생성 // xxx.class @Bean public {Interface Name} {Me..
2024.03.14