Spring/스프링 핵심 원리 - 기본편
-
[인프런] 스프링 핵심 원리 후기Spring/스프링 핵심 원리 - 기본편 2022. 8. 9. 00:19
이거 쓸려고 악착같이 다 들었다.. (약간 후기 쓰는게 로망이었음) 후반부로 갈수록 내용도 좀 어렵고 이해가 안 되는 부분도 있었지만 그래도 찾아가보면서 열심히 완강했다.. 제일 머리에 박힌 것은 '객체지향'을 생각하며 코드를 짜야겠다는 깨달음이다. 확실히 직접 만들어가면서 공부하는게 쉽게 지치지도 않고 재미도 있는 듯.. 이제 진짜 후기 ⇊⇊⇊⇊ 일단 강의 퀄리티 자체가 좋았다. 잡음도 없고, 수업 자료도 상세하게 설명되어 있어서 강의 듣기 전에 한 번 읽고 들으면 훨씬 이해가 잘 갔다. 또 강사님이 워낙 호감형(?)이신 분이시라 고등학생 때 선생님 좋아서 그 과목 열심히 하는 느낌으로 끝까지 들을 수 있었던 것 같기도 하다. 그리고 내가 이 강의를 듣기 시작했을 때 상태는 영한님 입문 강의랑 구글링해..
-
빈 스코프Spring/스프링 핵심 원리 - 기본편 2022. 8. 7. 23:04
[인프런] 스프링 핵심 원리 - 기본편 • 싱글톤 : 기본 스코프. 스프링 컨테이너의 시작과 종료까지 유지되는 가장 넓은 범위의 스코프 • 프로토타입 : 프로토타입 빈의 생성과 의존관계 주입까지만 관여하는 매우 짧은 범위의 스코프 • 웹 관련 스코프 ∘ request : 웹 요청이 들어오고 나갈때 까지 유지되는 스코프 ∘ session : 웹 세션이 생성되고 종료될 때 까지 유지되는 스코프 ∘ application : 웹의 서블릿 컨텍스트와 같은 범위로 유지되는 스코프 // 컴포넌트 스캔 자동 등록 @Scope("prototype") @Component public class HelloBean {} // 컴포넌트 스캔 수동 등록 @Scope("prototype") @Be..
-
빈 생명주기 콜백Spring/스프링 핵심 원리 - 기본편 2022. 8. 6. 18:30
[인프런] 스프링 핵심 원리 - 기본편 스프링은 의존관계 주입이 완료되면 스프링 빈에게 콜백 메서드를 통해서 초기화 시점을 알려주는 다양한 기능을 제공 스프링은 스프링 컨테이너가 종료되기 직전에 소멸 콜백을 줌 => 안전한 종료 진행 가능 초기화 콜백: 빈이 생성되고, 빈의 의존관계 주입이 완료된 후 호출 소멸전 콜백: 빈이 소멸되기 직전에 호출 📍@PostConstruct, @PreDestroy 📎 가장 편리한 초기화와 종료 실행 방법 📎 스프링에서 권장하는 방법 📎 스프링에 종속되지 않은 자바 표준임 (javax.annotation.PostConstruct) 📎 외부 라이브러리에는 적용하지 못함 => 외부 라이브러리를 초기화, 종료해야 하면 @Bean의 기능을 사용
-
의존관계 자동 주입Spring/스프링 핵심 원리 - 기본편 2022. 8. 6. 01:05
[인프런] 스프링 핵심 원리 - 기본편 📌 의존관계 주입 방법 생성자 주입 수정자 주입 (setter 주입) 필드 주입 일반 메서드 주입 📍생성자 주입 지금까지 실습을 통해 했던 방식이 생성자 주입 방식이었음 📎생성자 호출 시점에 1번만 호출되는 것을 보장 📎불변, 필수 의존관계에 사용 📎생성자가 1개만 있으면 @Autowired를 생략해도 자동 주입됨 @Component public class OrderServiceImpl implements OrderService { private final MemberRepository memberRepository; private final DiscountPolicy discountPolicy; @Autowired public OrderServiceImpl(Mem..
-
컴포넌트 스캔Spring/스프링 핵심 원리 - 기본편 2022. 8. 5. 10:47
[인프런] 스프링 핵심 원리 - 기본편 @Component @Autowired 📍 일일이 스프링 빈을 등록하는 것은 누락, 귀찮음 등의 문제 발생시킴! @Configuration @ComponentScan(excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = Configuration.class)) // 기존 코드를 수정하지 않고 남겨두기 위해 exclude함 public class AutoAppConfig { } 📎 @ComponentScan : @Component 애노테이션이 붙은 클래스를 스캔해서 스프링 빈으로 등록함 : 사용하는 각 클래스에 @Component를 붙이면 됨 📎@Autowired : 자동으로 의존관계 주입을 해줌 ↪ 생성자..
-
싱글톤 컨테이너Spring/스프링 핵심 원리 - 기본편 2022. 8. 4. 17:57
[인프런] 스프링 핵심 원리 - 기본편 public class SingletonService { // static으로 선언했기 때문에 하나만 만들어져서 올라감 private static final SingletonService instance = new SingletonService(); // 객체를 만들어 조회할 때 사용 // instance의 참조를 꺼낼 수 있는 방법은 이 메소드 밖에 없음 public static SingletonService getInstance() { return instance; } // private로 선언했기 때문에 외부에서 생성 불가 private SingletonService() { } public void logic() { System.out.println("싱글톤 ..
-
스프링 컨테이너와 스프링 빈Spring/스프링 핵심 원리 - 기본편 2022. 8. 4. 01:40
[인프런] 스프링 핵심 원리 - 기본편 스프링 빈 조회 스프링 빈을 찾는 가장 기본적인 조회 방법 ac.getBean(빈 이름, 타입) ac.getBean(타입) 📎조회 대상 스프링 빈이 없으면 발생하는 예외 NoSuchBeanDefinitionException: No bean named 'xxxxx' available 📎타입으로 조회할 때 같은 타입의 스프링 빈이 두 개 이상 있으면 오류 발생 => 빈 이름 지정하기 ac.getBeanOfType() ↪ 해당 타입의 모든 빈 조회 가능 스프링 빈을 조회할 때 상속관계 부모 타입으로 조회하면, 자식 타입도 모두 함께 조회됨 => Object로 조회하면 모든 스프링 빈 조회함 ⭐ 📍BeanFactory와 Application..
-
스프링 핵심 원리 이해2 - 객체 지향 원리 적용Spring/스프링 핵심 원리 - 기본편 2022. 8. 3. 14:39
[인프런] 스프링 핵심 원리 - 기본편 할인 정책 변경 요청 => 금액당 할인 RateDiscountPolicyTest @DisplayName • 클래스와 메소드에 사용 가능 • 테스트의 이름을 텍스트로 설정함 할인 정책을 변경하려면 OrderServiceImpl의 new 문의 코드를 고쳐야 함 📎문제점 OrderServiceImpl은 DiscountPolicy 추상 인터페이스에 의존하긴 하지만, 구현 클래스인 FixDiscountPolicy와 RateDiscountPolicy에도 의존하고 있음 => DIP 위반 + OCP 위반 📎해결방법 인터페이스에만 의존하도록 변경하면 됨 private DiscountPolicy discountPolicy; // new 객체를 삭제 (인터페이스 문만 남김) 인터페이스..