Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 스프링사진
- 스프링부트사진올리기
- 스프링부트구독취소
- 스프링부트서버에사진전송
- ssh도커설치
- 출처 따배도
- 도커설치하는법
- 출처 노마드코더
- 출처 코딩셰프
- 스프링부트
- 멀티폼
- 파이썬sort
- 스프링구독
- 출처 문어박사
- 스프링사진업로드
- 스프링부트api
- vm도커설치하는법
- 스프링이미지업로드
- 우분투도커설치
- 스프링부트팔로우취소
- 스프링부트중복예외처리
- centos도커설치
- 스프링익셉션처리
- 스프링부트팔로잉
- dockerinstall
- 서버에도커설치
- 인스타클론
- springboot_exception_handler
- WAS웹서버
- 출처 메타코딩
Archives
- Today
- Total
MakerHyeon
[Spring 기초] DI Application 본문
DI 용어
● 빈 (Bean)
- 스프링에 의하여 관리당하는 자바 객체. 스프링 빈이면 모두 같은 인스턴스다.
- 스프링 IOC 방식으로 관리하는 오브젝트
● 빈 팩토리 (BeanFactory)
- 스프링 IOC를 담당하는 핵심 컨테이너
● 어플리케이션 컨텍스트 (ApplicationContext)
- BeanFactory를 상속
- Bean의 생성과 관리를 담당별도의 정보를 참고해서 빈(오브젝트)의 생성, 관계설정 등의 제어 작업을 총괄 (설정 메타정보 (Configuration Metadata))
1. POJO 파일생성
- Printer를 의존하고 있는 클래스인 Hello 클래스
- Printer 인터페이스
- Printer 인터페이스를 상속하는 클래스 2개 작성
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="myspring.di.xml.Hello">
<property name="name" value="Spring"/>
<property name="printer" ref="printer"></property>
</bean>
<bean id="printer" class="myspring.di.xml.StringPrinter"/>
<bean id="consolePrinter" class="myspring.di.xml.ConsolePrinter"/>
</beans>
<bean id> : bean 이름
<bean class> : 사용할 클래스 이름
<property name> : 클래스 멤버명
<Property value> : 넣어줄 값
<Property ref> : 참조할 빈
DI 테스트 클래스
package myspring.di.xml.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import myspring.di.xml.Hello;
import myspring.di.xml.Printer;
public class HelloBeanTest {
public static void main(String[] args) {
// 1. IoC 컨테이너 생성
ApplicationContext context =
new GenericXmlApplicationContext("config/beans.xml");
// 2. Hello Bean 가져오기
//Object 타입으로 가져오기 때문에 Hello 객체로 형변환 필요
Hello hello =(Hello)context.getBean("hello");
System.out.println(hello.sayHello());
hello.print();
// 3. StringPrinter Bean 가져오기
Printer printer = (Printer)context.getBean("printer");
System.out.println(printer.toString());
// 4. Singleton 패턴 확인
Hello hello2 = context.getBean("hello",Hello.class);
System.out.println(hello == hello2);
//True -> IoC 컨테이너가 Spring Bean을 Singleton 패턴으로 관리한다!
}
}
context.getBean 사용 시...
- id만 사용시 : 형변환이 필요
- id와 클래스를 같이 지정해줄 시 : 형변환을 따로 해주지 않아도 됨
'SpringBoot' 카테고리의 다른 글
[Spring 기초] jUnit 개념과 Annotation (0) | 2023.05.10 |
---|---|
[SpringBoot] URI를 이용한 REST API Version 관리 (0) | 2023.03.05 |
[SpringBoot] 프로그래밍으로 제어하는 Filtering 방법 - 사용자 조회 (0) | 2023.03.05 |
[instagramProject] 인스타 팔로우 / 좋아요 기능 구현 (0) | 2023.03.05 |
[SpringBoot] Response데이터 제어 Filtering (@JsonIgnore) (0) | 2023.03.03 |
Comments