본문 바로가기

노력/Spring Boot

스프링 프레임워크 핵심 기술 - (1)

인프런, 스프링 프레임워크 핵심 기술 강좌 (백기선님)

IoC(Inversion of Control), AOP(Aspect Oriented Programming) 처럼 추상 API에 다루는 강좌.

스프링의 역사

  • JavaEE와는 호환 관계이며 적대적 관계에 있는 것이 아니다.
  • 최근까지는 서블릿 기반 애플리케이션을 만들 때 사용, 하지만 Spring 5부터는 WebFlux 지원으로 서블릿 기반이 아닌 서버 애플리케이션도 개발할 수 있게 됨.

 

디자인 철학

  • 모든 선택은 개발자의 몫.
  • 다양한 관점을 지향.
  • 하위 호환선을 지킴.
  • API를 신중하게 설계.
  • 높은 수준의 코드 지향.

 

 

1부: 스프링 IoC 컨테이너와 빈

@Service
public class SomeService {
  private SomeRepository someRepository;

  public SomeService(SomeRepository someRepository) {
      this.someRepository = someRepository;
  }
}

위 소스코드처럼 BookRepository 객체를 new를 사용해 생성하는 것이 아닌 생성자를 사용해 등록하는 것을 IoC(제어의 역전)이라고 한다.

 

빈(스프링 IoC 컨테이너가 관리하는 객체)들을 담고 있기 때문에 컨테이너라고 부름.

 

xml 기반의 빈 주입을 사용하다가 현재는 애노테이션(@)을 사용한 빈 주입을 하고 있음.

 

IoC 주입의 가장 핵심이되는 인터페이스는 아래의 BeanFactory.

https://docs.spring.io/spring-framework/docs/5.0.8.RELEASE/javadoc-api/org/springframework/beans/factory/BeanFactory.html

 

BeanFactory (Spring Framework 5.0.8.RELEASE API)

The root interface for accessing a Spring bean container. This is the basic client view of a bean container; further interfaces such as ListableBeanFactory and ConfigurableBeanFactory are available for specific purposes. This interface is implemented by ob

docs.spring.io

 

  • 스프링 IoC 컨테이너가 관리하는 객체.
  • 스코프 (싱글톤, 프로토타입)
  • 의존성 관리
  • 라이프사이클 인터페이스 (@PostConstruct)

ApplicationContext (자주 사용하게 될 IoC 컨테이너)

  • BeanFactory
  • 메시지소스 처리기능
  • 이벤트 발행 기능
  • 리소스 로딩 기능
  • ...

 

ApplicationContext

 

ApplicationContext (Spring Framework 5.0.8.RELEASE API)

Expose AutowireCapableBeanFactory functionality for this context. This is not typically used by application code, except for the purpose of initializing bean instances that live outside of the application context, applying the Spring bean lifecycle (fully

docs.spring.io

스프링 IoC 컨테이너의 역할

  • 빈 인스턴스 생성
  • 의존 관계 설정
  • 빈 제공

 

ApplicationContext

  • ClassPathXmlApplictaionContext(XML)
  • AnnotaionConfigApplicationContext(Java)

 

ApplicationContext 컨테이너 설정은 역사에 따라 달라져 왔다.

  1. xml 기반 직접 설정

    ClassPathXmlApplictaionContext 직접 bean 설정
    <bean id="bean1" class="packageName.Class">​


  2. xml 기반 스캔

    ClassPathXmlApplictaionContext 패키지 component-scan
    <context:component-scan base-package="packageName"/>​


  3. Java Configuration 직접 설정
    @Configuration
    public class ApplicationConfig {
    
        @Bean
        public SomeRepository someRepository() {
            return new SomeRepository();
        }
        
        @Bean
        public SomeService someService(SomeRepository someRepository) {
            SomeService someService = new SomeService();
            someService.setSomeRepository(someRepository);
            return new SomeService();
        }
    }​
    AnnotaionConfigApplicationContext를 사용하고 Config 파일에서 @Configuration과 @Bean 어노테이션으로 직접 Bean들을 추가하는 방법

  4. Java ComponentScan 설정
    @ComponentScan(basePackageClasses = DemoApplication.class)
    public class ApplicationConfig {
    
        @Bean
        public SomeRepository someRepository() {
            return new SomeRepository();
        }
    
        @Bean
        public SomeService someService() {
            return new SomeService();
        }
    }​
    AnnotaionConfigApplicationContext를 사용하고 Config 파일에서 @ComponentScan 사용

  5. Spring Boot
    @SpringBootApplication을 사용하여 각 빈에서 어노테이션 기반으로 설정.