SPRING/chapter02_DI
DI_12_compoent : LgTV.JAVA/TV.JAVA/TVUser.JAVA/applicationContext12.xml
GAWON
2023. 6. 27. 10:39
package org.joonzis.DI_12_compoent;
import org.springframework.stereotype.Component;
/*
@Component와 @Configuration/@Bean 은 기능이 비슷
@Component은 클래스 단위
@Bean은 메소드 단위
*/
/*
* java => @Component("tv")
* xml => <bean id="tv" class="org.joonzis.DI_12_component.LgTV"/>
*/
@Component("tv")
public class LgTV implements TV{
public LgTV() {
System.out.println("--> LgTV 객체 생성");
}
@Override
public void powerOff() {
System.out.println("--> LgTV 전원 끈다");
}
@Override
public void powerOn() {
System.out.println("--> LgTV 전원 켠다");
}
@Override
public void volumeDown() {
System.out.println("--> LgTV 소리 내린다.");
}
@Override
public void volumeUp() {
System.out.println("--> LgTV 소리 울린다.");
}
}
package org.joonzis.DI_12_compoent;
public interface TV {
public void powerOn();
public void powerOff();
public void volumeUp();
public void volumeDown();
}
package org.joonzis.DI_12_compoent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class TVUser {
public static void main(String[] args) {
AbstractApplicationContext ctx =
new GenericXmlApplicationContext("applicationContext12.xml");
TV tv = (TV)ctx.getBean("tv");
tv.powerOff();
tv.volumeDown();
tv.volumeUp();
tv.powerOn();
ctx.close();
}
}
<?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"
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">
<!--
컴포넌트 스캔(component-scan) 설정
사용할 객체들을 <bean> 등록하지 않고, 자동으로 생성하기 위해 사용
@Component가 설정된 클래스들을 자동으로 객체 생성한다.
*bean-package : 해당 패키지로 시작하는 모든 패키지를 스캔 대상에 포함
-> org.joonzis.*
<context:component-scan base-package="org.joonzis"/>
아래 선언한 태그는 클래스이 충동을 막기위해 해당 패키지 한정으로 처리
-->
<context:component-scan base-package="org.joonzis.DI_12_component"></context:component-scan>
</beans>