SeSAC 65일차(2023-10-24)
Spring
- scope
- 싱글톤
- 기본 scope
- 스프링 컨테이너의 시작과 종료까지 유지되는 가장 넓은 범위의 scope
- 프로토타입
- bean의 생성과 의존 관계 주입까지만 관여하는 짧은 범위의 scope
- 웹 scope
- request scope
- 다음 요청까지만 객체 유지
- session scope
- 웹 세션이 생성되고 종료될 때 까지 유지
- application scope
- request scope
- 싱글톤
- 스프링 Containter 구성 방법
- xml 기반 (legacy)
- Annotation 기반
package dev.spring.step01field; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyRoom { public static void main(String[] args) { // 1. XML 파일을 사용하지만 Annotation이 적용되어 보다 간소화된 방식, field 기반 주입 ApplicationContext context = new ClassPathXmlApplicationContext("annotation-config-field.xml"); TapeReader reader = context.getBean(TapeReader.class); System.out.println(reader); reader.test(); } }
package dev.spring.step03constructor; import org.springframework.beans.factory.annotation.Value; public class Tape { @Value("아일랜드") private String name; // 필드 기반 기본 값 지정 @Value("true") private boolean isWorked; // true, false로 바꿔보기 public Tape() { super(); System.out.println("step03constructor Tape() called"); } public Tape(String name, boolean isWorked) { super(); this.name = name; this.isWorked = isWorked; } public String getName() { return name; } public void setName(String name) { System.out.println("setName() called"); this.name = name; } public boolean isWorked() { return isWorked; } public void setWorked(boolean isWorked) { System.out.println("setWorked() called"); this.isWorked = isWorked; } }
- 필드 주입(Field Injection)
package dev.spring.step01field; import org.springframework.beans.factory.annotation.Autowired; public class TapeReader { **@Autowired // 의존성을 자동으로 연결해주는 옵션 private Tape tape;** public void test() { if (tape.isWorked()) { System.out.println(tape.getName() + " 정상 동작합니다"); } else { System.out.println(tape.getName() + " 사기 당했습니다"); } } public void setTape(Tape tape) { System.out.println("setType(Tape tape) called"); this.tape = tape; } @Override public String toString() { return "TapeReader 입니다."; } }
- 수정자 주입(Setter Injection)
@Autowired // 의존성을 자동으로 연결해주는 옵션 public void setTape(Tape tape) { System.out.println("setType(Tape tape) called"); this.tape = tape; }
- 생성자 주입(Constructor Injection)
@Autowired // 의존성을 자동으로 연결해주는 옵션 public TapeReader(Tape tape) { super(); this.tape = tape; }
- Java 파일 기반(코드)
- component scan
- MyRoom.java, TapeReader.java의 형태는 동일
- Tape.java
package dev.spring; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component // 해당 클래스를 bean으로 등록, 스캐닝 대상에 포함시킴 public class Tape { private String name; // 필드 기반 기본 값 지정 private boolean isWorked; // true, false로 바꿔보기 // 기본 생성자 지우면 주입, 기본 생성자 있을때는 여기다가 @Autowired 적으면 이 생성자로 주입됨 public Tape(@Value("아일랜드") String name, @Value("true") boolean isWorked) { super(); this.name = name; this.isWorked = isWorked; } public String getName() { return name; } public boolean isWorked() { return isWorked; } }
- component-scan-config.xml
오늘의 실습 코드: https://github.com/yubin-im/SeSAC/tree/main/20231024
728x90
'교육 (Today I Learned) > SeSAC' 카테고리의 다른 글
SeSAC 67일차 / JPA (0) | 2023.11.01 |
---|---|
SeSAC 66일차 / Spring (0) | 2023.11.01 |
SeSAC 64일차 / servlet (0) | 2023.10.24 |
SeSAC 63일차 / JavaScript (0) | 2023.10.24 |
SeSAC 62일차 / JavaScript (0) | 2023.10.24 |