LoginSignup
0
0

More than 1 year has passed since last update.

Mr.Nam 先生の講義要約 Chapter 3, 1-8

Posted at

1강 Spring DI 흉내내기 (1)
1)변경에 유리한 코드(1) - 다형성, factory method
//변경 포인트 2개
SportsCar car = new SportsCar();
--> Truck car = new Truck();

==>

//다형성 --> 변경에 유리
//변경 포인트 1개
Car car = new SportsCar();
--> Car car = new Truck();

==>

Car car = getCar(); // getCar()메서드 하나만 고치면 됨
static Car getCar() {
return new SportsCar();
}
2)변경에 유리한 코드(2) - Map과 외부 파일

2강 Spring DI 흉내내기 (2)
객체 컨테이너(ApplicationContext) 만들기
Map map 객체 저장소
Properties에 저장된 내용을 Map에 저장
반복문으로 클래스 이름을 얻어서 객체를 생성해서 다시 map에 저장
자동 객체 등록하기 - Component Scanning
@Component

  1. 패키지 내의 모든 클래스를 읽어서 Set에 저장
  2. 반복문으로 클래스를 하나씩 읽어와서 @Component이 붙어 있는지 확인
  3. @Component가 붙어있으면 객체를 생성해서 map에 저장

3강 Spring DI 흉내내기 (3)
객체 찾기 - by Name, by Type
AppContext ac = new AppContext();
Car car = (Car)ac.getBean("car"); //이름(id)으로 찾기
Car ca2 = (Car)ac.getBean("Car.class"); //타입을 찾기

객체를 자동 연결 하기(1) - @Autowired

  1. 수동 연결
  1. 자동 연결
    @Autowired
    맵을 뒤져서 각 타입에 맞는 객체주소를 찾아서 참조변수에 대입.
    by Type, Key값을 찾음
    @Resource
    by Name, instanceof로 Value값을 찾음.
    타입의 첫글자를 소문자로 바꿔서 찾음.
    @Resource(name="engine2")을 써서 찾을 수도 있음.

4강 Spring DI 활용하기 - 실습
1)config1.xml
scope="prototype"은 매번 새로운 객체를 만듦
Constructor-arg태그는 setter를 사용
String, 기본형일 때 value 사용
참조형은 ref 사용

2)SpringDiTest.java
Qualifier("superEngine") //타입으로 먼저 검색 후 n개면 이름으로 검색
@Resource(name="superEngine") //byName
Engine engine; //byType - 타입으로 먼저 검색, 여러개면 이름으로 검색. - engine, superEngine, turboEngine

5강 Spring DI 활용하기 - 이론(1)
1)빈(bean)이란?
JavaBeans
재사용 가능한 컴포넌트, 상태(iv), getter&setter, no-args constructor

Servlet & JSP bean
MVC의 Model, EL, scope, JSP container가 관리

EJB(Enterprise Java Beans)
복잡한 규칙, EJB container가 관리

Spring Bean
POJO(Plain Old Java Object). 단순, 독립적, Spring container가 관리

2)BeanFactory와 ApplicationContext
Bean
Spring Container가 관리하는 객체
xml에 빈을 정의 -> Spring Container

Spring Container
Bean 저장소, Bean을 저장, 관리(생성, 소멸, 연결)@Autowired

BeanFactory : Bean을 생성, 연결 등의 기본 기능을 정의한 인터페이스
ApplicationContext : BeanFactory를 확장해서 여러 기능을 추가 정의
(Spring Container와 ApplicationContext는 비슷하다 보면 됨)

6강 Spring DI 활용하기 - 이론(2)

  1. ApplicationContext의 주요 메서드
    getBean() //빈 얻기
    isTypeMatch() //타입 확인
    containsBean() //빈이 있는지 확인

7강 Spring DI 활용하기 - 이론(3)
1)loC와 DI
제어의 역전 IoC(Inversion of Control)
제어의 흐름을 전통적인 방식과 다르게 뒤바꾸는 것.
[전통적인 방식] 사용자 코드가 Framework 코드를 호출
[loc] Framework 코드가 사용자 코드를 호출

2)의존성 주입 DI(Dependency Injection)
사용할 객체를 외부에서 주입받는 것.
(IoC, DI는 디자인패턴의 전략패턴)
수동 주입, 자동 주입이 있음!

8강 Spring DI 활용하기 - 이론(4)
1)스프링 애너테이션 - @Resource
Spring container에서 "이름(by Name)"으로 빈을 검색해서 참조변수에 자동 주입(DI).
일치하는 이름의 빈이 없으면, 예외 발생.

2)스프링 애너테이션 - @Component
@Component가 클래스를 자동 검색해서 빈으로 등록

3)@Controller, @Service, @Repository, @ControllerAdvice의 메타 애너테이션
을 하면 @Controller, @Service, @Repository, @ControllerAdvice가 붙으면 자동 등록됨.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0