📑 개발 이론
스프링 핵심 원리 - 싱글톤
※ 본 포스팅은 21.10.15에 게시된 글을 이전한 것 입니다. 1. 싱글톤 패턴 클래스의 인스턴스가 딱 1개만 생성되는 것을 보장하는 디자인 패턴이다. 1) 필요 이유 웹 애플리케이션은 보통 여러 고객이 동시에 요청을 한다. 싱글톤 패턴으로 설계하지 않은 프로젝트는 고객이 요청할 때마다 객체를 새로 생성한다. 다시 말해서 메모리 낭비가 심하다. 그래서 해당 객체가 오직 1개만 생성되고 공유하도록 설계하는 싱글톤 패턴을 사용하면 된다. 요즘 컴퓨터가 좋아서 속도는 늦지 않지만, 효율적으로 관리하기 위해 싱글톤 패턴을 사용한다. 2) 싱글톤 코드 package hello.core.singleton; public class SingletonService { private static final Single..
스프링 핵심 원리 - 스프링 빈
※ 본 포스팅은 21.10.14에 게시된 글을 이전한 것 입니다. 1. 스프링 빈 조회 1) 모든 빈 조회 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); ~ String[] beanDefinitionNames = ac.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { Object bean = ac.getBean(beanDefinitionName); System.out.println("Name = " + beanDefinitionName+" object = "+bean); } .getB..
스프링 핵심 원리 - 스프링 컨테이너
※ 본 포스팅은 21.10.14에 게시된 글을 이전한 것 입니다. 1. 스프링 컨테이너 생성 `ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);` ApplicationContext가 스프링 컨테이너이며 인터페이스이다.(다형성 보존) 스프링 컨테이너는 XML 기반으로 만들 수도 있고 애노테이션 기반의 자바 설정 클래스로 만들 수 있다.(위 코드가 자바 설정 클래스) ※ 좀 더 정확히는 스프링 컨테이너를 부를 때 BeanFactory , ApplicationContext로 구분해서 부른다. 하지만 일반적으로 ApplicationContext를 스프링 컨테이너라고 부른다. ※ Appcon..
스프링 핵심 원리 - 객체지향 원리 적용 정리
※ 본 포스팅은 21.10.13에 게시된 글을 이전한 것 입니다. SRP 단일 책임 원칙 단일 책임의 원칙을 지키기 위해 관심사 분리 구현 객체를 생성하고 연결하는 책임은 AppConfig 담당 클라이언트 객체는 실행하는 책임만 담당 DIP 의존관계 역전 원칙 "추상화에 의존해야지, 구체화에 의존하면 안된다."를 따르기 위해 의존성 주입을 사용함 private final MemberRepository memberRepository = new MemoryMemberRepository(); MemoryMemberRepository() 구체화에 의존했다. 하지만 클라이언트는 추상화만 의존해서는 아무것도 실행할 수 없어 AppConfig가 객체 인스턴스를 클라이언트 코드 대신 생성해 클라이언트 코드에 의존관계를..
[스프링 핵심원리] OCP와 DIP
package hello.core.member; public class MemberServiceImpl implements MemberService { private final MemberRepository memberRepository = new MemoryMemberRepository(); public void join(Member member) { memberRepository.save(member); } public Member findMember(Long memberId) { return memberRepository.findById(memberId); } } 설계상 문제점은 아래와 같다. private final MemberRepository memberRepository = new Memor..