기록해! 정리해!

Setter 사용하기 (1) 본문

Spring

Setter 사용하기 (1)

zsuling 2022. 9. 20. 09:46

[ applicationContext.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 
	http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 생성자 인젝션 사용하기  

<bean id="SamSungTV" class="polymorphism.SamSungTV" >
  <constructor-arg ref="apple" />
  <constructor-arg value="2500000" />
</bean>
<bean id="LgTV" class="polymorphism.LgTV" >
  <constructor-arg ref="sony"/>
  <constructor-arg value="3500000" />
</bean>

 -->
 
 <!--  Setter 인젝션 사용하기 -->
 <bean id="SamSungTV" class="polymorphism.SamSungTV" >
   <property name="speaker" ref="apple" />
   <property name="price"  value="2500000" />

</bean>
<bean id="LgTV" class="polymorphism.LgTV" >
   <property name="speaker" ref="sony" />
   <property name="price"  value="3500000" />
</bean>
 
 
<bean id="apple" class="polymorphism.AppleSpeaker" />
<bean id="sony" class="polymorphism.SonySpeaker" />

</beans>

package polymorphism;

public class SamSungTV implements TV{
	
	private Speaker speaker;
	private int     price = 0;
	SamSungTV(){
		System.out.println("SamSungTV 생성자");
	}
	
	SamSungTV( Speaker speaker ){
		System.out.println("SamSungTV 생성자(1)");
		this.speaker = speaker;
	}
	
	SamSungTV( Speaker speaker, int price ){
		System.out.println("SamSungTV 생성자(2)");
		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 powerOn() {
		System.out.println("==>SamSungTV powerOn");
		System.out.println("가격:"+price);
		
	}

	@Override
	public void powerOff() {
		System.out.println("==>SamSungTV powerOff");
		
	}

	@Override
	public void volumeUp() {
		// System.out.println("SamSungTV volumeUp");
		speaker.volumeUp();
		
	}

	@Override
	public void volumeDown() {
		// System.out.println("SamSungTV volumeDown");
		speaker.volumeDown();
	}

}

 

'Spring' 카테고리의 다른 글

어노테이션 기반 설정  (0) 2022.09.20
Setter 사용하기 (2) - p 네임스페이스 사용하기  (0) 2022.09.20
Spring - 서블릿 클래스 확인  (0) 2022.09.20
ajax + jQuery + 댓글  (0) 2022.09.19
MyBatis  (0) 2022.09.08
Comments