1) SystemMonitor 클래스

package com.model.di.exam05;


public class SystemMonitor {

private int periodTime;

private SmsSender sender;

// 생성자 이용

public SystemMonitor(int periodTime, SmsSender sender){

this.periodTime = periodTime;

this.sender = sender;

}

public void write(){

System.out.println("SystemMonitor2 [periodTime="+periodTime

+", sender="+sender+"]");

}

}


2) SmsSender 클래스

package com.model.di.exam05;


public class SmsSender {


@Override

public String toString() {

return "SmsSender 호출";

}

}



3) 실행 클래스

package com.model.di.exam05;


import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {

public static void main(String[] args) {

AbstractApplicationContext context = 

new ClassPathXmlApplicationContext("applicationContext.xml");

context.registerShutdownHook();

SystemMonitor monitor = (SystemMonitor)context.getBean("monitor2");

monitor.write();

context.close();

}

}




4) 설정 파일


<?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"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd">


<!-- com.model.di.exam01 -->

<bean id="messageBean" class="com.model.di.exam01.MessageBean"></bean>

<!-- com.model.di.exam02 -->

<bean id="service" class="com.model.di.exam02.ServiceImpl">

<constructor-arg>

<ref bean="daoImpl"/>

</constructor-arg>

</bean>

<bean id="table" class="com.model.di.exam02.DbTable"></bean>

<bean id="daoImpl" class="com.model.di.exam02.DaoImpl" ></bean>

<!-- com.model.di.exam04 -->

<bean id="sender" class="com.model.di.exam04.SmsSender" />

<!-- 1) property 속성으로 속성값 주입 -->

<!-- <bean id="monitor" class="com.model.di.exam04.SystemMonitor">

<property name="periodTime" value="10" />

<property name="sender" ref="sender" />

</bean> -->

<!-- 2) p(namespaces)를 이용한 속성값 주입 -->

<bean id="monitor" class="com.model.di.exam04.SystemMonitor" 

p:periodTime="10" p:sender-ref="sender"/>

<!-- com.model.di.exam05 -->

<bean id="sender2" class="com.model.di.exam05.SmsSender"></bean>

<bean id="monitor2" class="com.model.di.exam05.SystemMonitor">

<constructor-arg value="10" />

<constructor-arg ref="sender2"/>

</bean>


</beans>



Posted by netyhobby
,