하나씩 차근차근
의존관계 자동주입
Spring/Spring 핵심 원리 2023. 1. 21. 11:46

앞에서 스프링 컨테이너가 @Autowired 애노테이션을 찾아서 스프링 빈을 자동으로 주입했습니다. 이렇게 의존관계를 주입하는것은 아래와 같이 다양한 방법이 있습니다. 생성자 주입 수정자 주입 필드 주입 일반 메서드 주입 롬복을 사용한 주입 생성자 주입 생성자 주입 방법은 지금까지 사용한 방법으로 생성자 호출시점에 딱 1번 호출됩니다. package hello.core.order; ... @Component public class OrderServiceImpl implements OrderService{ private final MemberRepository memberRepository; private final DiscountPolicy discountPolicy; @Autowired public O..

article thumbnail
컴포넌트 스캔
Spring/Spring 핵심 원리 2023. 1. 21. 11:19

앞에서 스프링 빈을 등록할때 스프링 컨테이너를 @Configuration 과 @Bean 을 사용하는 방법과 XML 을 통해 만드는 방법으로 스프링 빈을 등록했습니다. 만약 등록해야할 스프링 빈이 많아지면 설정 정보가 커지고 누락하는 문제가 발생합니다. 그래서 자동으로 스프링 빈을 등록하는 컴포넌트 스캔을 사용해보겠습니다. 시작 컴포넌트 스캔을 사용하면 @Configuration 이 붙은 설정 정보도 등록되기 때문에 Appconfig 에 붙은 @Configuration 과 @Bean 을 지워줍니다. package hello.core; import hello.core.member.MemberRepository; import hello.core.member.MemberService; import hello.c..

article thumbnail
스프링 컨테이너와 스프링 빈
Spring/Spring 핵심 원리 2023. 1. 20. 15:41

앞에서 만든 예제 코드에 스프링을 사용해보겠습니다. 시작 먼저 Appconfig 에 @Configuration 과 @Bean 애노테이션을 붙여주겠습니다. package hello.core; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import hello.core.member.MemberRepository; import hello.core.member.MemberService; import hello.core.member.MemberServiceImpl; import hello.core.member.MemoryMemberRepository; imp..

article thumbnail
예제 만들기 - AppConfig 생성
Spring/Spring 핵심 원리 2023. 1. 20. 15:05

앞에서 만든 OrderServiceImpl 구현체를 보면 다음과 같이 1000원을 할인해주는 FixDiscountPolicy 적용해서 사용하고 있습니다. package hello.core.order; public class OrderServiceImpl implements OrderService{ private final DiscountPolicy discountPolicy = new FixDiscountPolicy(); ... } 만약 금액에 따른 할인을 하는 RateDiscountPolicy 를 적용하게 된다면 아래와 같이 OrderServiceImpl 을 수정해야합니다. package hello.core.order; public class OrderServiceImpl implements Order..

article thumbnail
예제 만들기 - 주문과 할인 도메인 설계
Spring/Spring 핵심 원리 2023. 1. 20. 13:24

다음으로 주문과 할인 도메인을 작성하겠습니다. 요구사항 회원은 상품을 주문 회원 등급에 따라 할인을 받을 수 있다. 할인은 모든 VIP 를 1000원 할인해주는 할인과 주문 금액당 할인을 해주는 두 가지 방법 주문 엔티티 다음과 같이 주문 엔티티를 작성하기 위해 order 패키지를 만들어 그 안에 생성합니다. package hello.core.order; public class Order { private Long memberId; private String itemName; private int itemPrice; private int discountPrice; public Order(Long memberId, String itemName, int itemPrice, int discountPrice) ..

article thumbnail
예제 만들기 - 회원 도메인 설계
Spring/Spring 핵심 원리 2023. 1. 14. 02:51

이번 포스트에서는 회원 도메인의 요구사항을 토대로 설계를 해보겠습니다. 요구사항 회원가입과 조회 회원등급은 일반과 VIP 회원 데이터는 자체 DB 또는 외부 시스템과 연동 회원 엔티티 먼저 회원등급을 위한 Grade 라는 eum 을 member 패키지를 하나 만들어서 생성합니다. package hello.core.member; public enum Grade { BASIC, VIP } 다음으로 회원 도메인을 위한 Member 클래스를 생성합니다. package hello.core.member; public class Member { private Long id; private String name; private Grade grade; public Member(Long id, String name, Gr..

article thumbnail
예제 만들기 - 프로젝트 생성
Spring/Spring 핵심 원리 2023. 1. 14. 02:40

이번 포스트부터 인프런에서 김영한님의 스프링 핵심 원리 - 기본편을 들으면서 배운 내용을 정리해보려 합니다. 스프링을 공부하지 않고 스프링부트를 먼저 공부했기 때문에 스프링부트의 기본이 되는 스프링을 공부하며, 스프링부트가 왜 생겼는지를 알아보려고 합니다. 프로젝트 생성 프로젝트 개발환경은 다음과 같습니다. JAVA 11 Gradle (빌드도구) Packaging : jar Dependencies : 선택하지 않음 위와 같이 설정을 해서 프로젝트를 생성합니다. 프로젝트 생성이 완료되었으면 프로젝트를 실행시켜봅니다. 다음과 같이 콘솔창에 출력이 되면 완료됩니다. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ ..