Spring 핵심원리

JY·2025년 8월 10일
0

Spring

목록 보기
5/5

📌 Spring Container 생성

// App

ApplicationContext applicationContext = 
	new AnnotationConfigApplicationContext(xxx.class);
  1. ApplicationContext를 스프링 컨테이너라고 한다.
  2. SpringContainer는 XML 방식, Annotation 방식의 2가지 방식으로 생성할 수 있다.
  3. ApplicationContext는 Interface이다.
  4. new AnnotationConfigApplicationContext()는 Interface의 구현 객체이고, 파라미터로 Class 파일을 받는다.

📌 Bean 생성

// xxx.class

@Bean
public {Interface Name} {Method Name}() {
    return new {Object Name}();
}
  1. SpringContainer에서 관리하는 Interface로 구현된 자바 객체를 Spring Bean이라고 한다.
  2. SpringContainer 내부에는 Bean Storage가 존재한다.
  3. Bean Storage는 xxx.class에 명시된 Bean이름(Method name)과 Bean객체를 각각 Key, Value값으로 가지게 된다.

Bean 이름은 @Bean(name="bean_name")으로도 설정 가능 & 중복 불가 !! > 다른 Bean을 무시 or 덮어쓰기

📌 Bean 의존 관계 설정

// xxx.class

@Bean
public Interface_1 method_1() {
    return new object_1(method_2);
}

@Bean
public Interface_2 method_2() {
	return new object_2();
}
  1. SpringContainer는 xxx.class 의 설정 정보를 참고하여 의존관계를 주입(DI)한다.
  2. 본래 Spring은 Bean 생성 단계와 DI 단계가 나누어져 있다.
  3. Java 코드를 통해 Bean을 등록하면 자동적으로 의존관계도 주입된다.

< GitHub >

https://github.com/Jingbug/SpringFramwork_Practice

0개의 댓글