2025-01-21






INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [applicationContext04.xml]
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:03:00 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@101952da: defining beans [tv,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,com.spring.injection04.SonySpeaker#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
==> SamsungTV(1) 생성
==> SonySpeaker 생성
SamsungTV --> 전원 키기 0
SonySpeaker --> 소리 올리기
SonySpeaker --> 소리 내리기
SamsungTV --> 전원 끄기
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@517cd4b: startup date [Tue Jan 21 21:03:00 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@101952da: defining beans [tv,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,com.spring.injection04.SonySpeaker#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
package com.spring.injection04;
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 개념이다.
// 애노테이션 이용
// 스프링 컨테이너를 생성하자.
GenericXmlApplicationContext container =
new GenericXmlApplicationContext("applicationContext04.xml");
// 컨테이터로 부터 객체를 검색(look up)한다
TV tv = (TV)container.getBean("tv"); // xml파일의 id를 가져온다
tv.turnOn(); // 전원을 켜는 메서드 호출
tv.soundUp(); // 볼륨을 올리는 메서드 호출
tv.soundDown(); // 볼륨을 내리는 메서드 호출
tv.turnOff(); // 전원을 끄는 메서드 호출
// 컨테이너를 종료 하면 컨테이너가 관리하는 모든 객체가 사라짐
container.close();
}
}
package com.spring.injection04;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/*
* IoC(Inversion Of Control : 제어의 역전 - 역제어 =/ 순제어)
* - 제어 : 객체(Object)에 대한 제어를 말함.
* - 제어 : Object에서 제어는 두 가지로 나뉘어 짐
* - 객체에 대한 생성(new 키워드)
* - 객체와 객체 간의 의존 관계
*/
@Component("tv") // id="tv" --> TVUser에서 lookup을 찾기 위한 id
public class SamsungTV implements TV{
@Autowired // Type Injection : 메모리 상의 "Speaker" 객체가 있으면 주소값을 변수에 할당
private Speaker speaker; // Speaker speaker = new SonySpeaker();
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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Annotation 기반의 IoC 설정 -->
<!-- base-package 의미는 com.spring.injection 패키지로 시작하는 모든
클래스들을 대상으로 @Component 애노테이션이 붙은 클래스들을 찾아라.
해당 클래스들의 객체를 메모리에 띄우라는 의미. -->
<context:component-scan base-package="com.spring.injection04"/>
<!-- 실제로 사용 할 Speaker 타입의 클래스 하나만 bean 으로 등록 -->
<!-- SamsungTV 클래스의 @Autowired를 사용한 Speaker speaker에 주소 값을 할당하는 작업 -->
<bean class="com.spring.injection04.SonySpeaker"/>
<!-- ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ -->
<!-- 객체의 변경이 잦은 경우는 bean 태그를 이용함. 또한 유지보수 측면에서
변경이 잦지 않은 경우는 애노테이션 사용 -->
<!-- 스프링 컨테이너가 생성할 객체를 bean으로 등록을 한다. -->
<!-- id : 컨테이너가 생성한 객체들을 식별하기 위해서 사용하는 이름(각각의 고유의 id 지정)
자바의 식별자 작성 규칙이 적용이 됨.
* 숫자로 시작하면 안됨
* 공백을 포함하면 안됨
* 특수기호를 사용하면 안됨
class : 패키지 경로가 포함된 클래스의 이름을 등록을 함.
-->
<!-- <bean id="speaker" class="com.spring.injection.SonySpeaker"/> -->
<!-- 생성자 주입(constructor injection) -->
<!-- <bean id="tv" class="com.spring.injection.SamsungTV">
<constructor-arg ref="speaker"/> 객체를 참조 할 시 ref 사용
<constructor-arg value="1500000"/>
</bean> -->
<!-- setter() 메서드를 이용한 주입 -->
<!-- <bean id="tv" class="com.spring.injection.SamsungTV">
<property name="speaker" ref="speaker"/>
<property name="price" value="1500000"/>
</bean> -->
<!-- namespaces 를 사용한 주입 -->
<!-- <bean id="tv" class="com.spring.injection.SamsungTV"
p:speaker-ref="speaker" p:price="1500000"/>
컬렉션에서 배열과 List 자료구조를 이용 시 주입 방법
<bean id="collection" class="com.spring.collection.CollectionBean">
<property name="addressList">
<map>
<entry>
<key><value>홍길동</value></key>
<value>91</value>
</entry>
<entry>
<key><value>세종대왕</value></key>
<value>100</value>
</entry>
<entry>
<key><value>유관순</value></key>
<value>93</value>
</entry>
<entry>
<key><value>김유신</value></key>
<value>77</value>
</entry>
<entry>
<key><value>신사임당</value></key>
<value>82</value>
</entry>
</map>
</property>
</bean> -->
</beans>'Spring, Boot > 기초 내용 정리' 카테고리의 다른 글
| Spring(DI)_02_01 (0) | 2025.01.21 |
|---|---|
| Spring(DI)_01_05 (1) | 2025.01.21 |
| Spring(DI)_01_03 (0) | 2025.01.21 |
| Spring(DI)_01_02 (0) | 2025.01.21 |
| Spring(DI)_01_01 (0) | 2025.01.21 |