2025-01-21




INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext03.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:00:52 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e4487af: defining beans [speaker,tv]; root of factory hierarchy
==> AppleSpeaker 생성
==> SamsungTV(1) 생성
--> setSpeaker() 메서드 호출
--> setPrice() 메서드 호출
SamsungTV --> 전원 키기 1500000
AppleSpeaker --> 소리 올리기
AppleSpeaker --> 소리 내리기
SamsungTV --> 전원 끄기
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:00:52 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e4487af: defining beans [speaker,tv]; root of factory hierarchy
package com.spring.injection03;
import org.springframework.context.support.GenericXmlApplicationContext;
/*
* 스프링 컨테이너
* - 컨테이너를 이용해서 애플리케이션 운영에 필요한 객체 생성을 스프링 컨테이널르
* 통해서 생성하는 것을 역제어(IoC) 라고 함.
* 스프링 컨테이너는 classpath 상에 xml 파일을 로딩해서 xml 기반으로
* xml 파일에 등록된 클래스의 객체를 메모리에 띄우고, 클라리언트는 컨테이너로 부터
* id 가 tv 이면서 동시에 tv 타입에 객체를 컨테이너로 부터 lookup 검색을 통해서
* 얻어낸 후에 lookup된 객체의 메서드를 이용해서 원하는 로직을 실행하는 것을 말함.
* 이것이 스프링의 IoC 개념임.
* - 그래서 컨테이너를 내가 만드는 것이 아니라 스프링이 제공하는 컨테이너를 이용하면 xml
* 만 수정하면 되기 때문에 유지보수 과정에서 자바 소스를 아무것도 건드리지 않고 실행되는
* 객체를 변경할 수 있음.
*/
public class TVUser {
public static void main(String[] args) {
// 현재 코드의 개념이 IoC 개념이다.
// 1. setter() 메서드를 이용한 주입
// 2. 네임스페이스를 사용한 주입
// 스프링 컨테이너를 생성하자.
GenericXmlApplicationContext container =
new GenericXmlApplicationContext("applicationContext03.xml");
// 컨테이터로 부터 객체를 검색(look up)한다
TV tv = (TV)container.getBean("tv"); // xml파일의 id를 가져온다
tv.turnOn(); // 전원을 켜는 메서드 호출
tv.soundUp(); // 볼륨을 올리는 메서드 호출
tv.soundDown(); // 볼륨을 내리는 메서드 호출
tv.turnOff(); // 전원을 끄는 메서드 호출
// 컨테이너를 종료 하면 컨테이너가 관리하는 모든 객체가 사라짐
container.close();
}
}
package com.spring.injection03;
/*
* IoC(Inversion Of Control : 제어의 역전 - 역제어 =/ 순제어)
* - 제어 : 객체(Object)에 대한 제어를 말함.
* - 제어 : Object에서 제어는 두 가지로 나뉘어 짐
* - 객체에 대한 생성(new 키워드)
* - 객체와 객체 간의 의존 관계
*/
public class SamsungTV implements TV{
private Speaker speaker;
private int price;
public SamsungTV() {
System.out.println("==> SamsungTV(1) 생성");
} // 기본 생성자
public SamsungTV(Speaker speaker) {
System.out.println("==> SamsungTV(2) 생성");
this.speaker = speaker;
} // 인자 생성자
public SamsungTV(Speaker speaker, int price) {
System.out.println("==> SamsungTV(3) 생성");
this.speaker = speaker;
this.price = price;
} // 인자 생성자
public void setSpeaker(Speaker speaker) {
System.out.println("--> setSpeaker() 메서드 호출");
this.speaker = speaker;
}
public void setPrice(int price) {
System.out.println("--> setPrice() 메서드 호출");
this.price = price;
}
@Override
public void turnOn() {
System.out.println("SamsungTV --> 전원 키기 " + price);
}
@Override
public void soundUp() {
//System.out.println("SamsungTV --> 소리 올리기");
speaker.volumeUp();
}
@Override
public void soundDown() {
//System.out.println("SamsungTV --> 소리 내리기");
speaker.volumeDown();
}
@Override
public void turnOff() {
System.out.println("SamsungTV --> 전원 끄기");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 스프링 컨테이너가 생성할 객체를 bean으로 등록을 한다. -->
<!-- id : 컨테이너가 생성한 객체들을 식별하기 위해서 사용하는 이름(각각의 고유의 id 지정)
자바의 식별자 작성 규칙이 적용이 됨.
* 숫자로 시작하면 안됨
* 공백을 포함하면 안됨
* 특수기호를 사용하면 안됨
class : 패키지 경로가 포함된 클래스의 이름을 등록을 함.
-->
<bean id="speaker" class="com.spring.injection03.AppleSpeaker"/>
<!-- setter() 메서드를 이용한 주입 -->
<bean id="tv" class="com.spring.injection03.SamsungTV">
<property name="speaker" ref="speaker"/>
<property name="price" value="1500000"/>
</bean>
<!-- 네임스페이스를 사용한 주입 -->
<!-- <bean id="tv" class="com.spring.injection03.SamsungTV"
p:speaker-ref="speaker" p:price="1500000"/>
<bean id="tv" class="com.spring.injection03.SamsungTV"
c:speaker-ref="speaker" c:price="1500000"/> -->
</beans>'Spring, Boot > 기초 내용 정리' 카테고리의 다른 글
| Spring(DI)_01_05 (1) | 2025.01.21 |
|---|---|
| Spring(DI)_01_04 (0) | 2025.01.21 |
| Spring(DI)_01_02 (0) | 2025.01.21 |
| Spring(DI)_01_01 (0) | 2025.01.21 |
| Spring(IoC)_03 (0) | 2025.01.21 |