기록해! 정리해!

어노테이션 기반 설정 본문

Spring

어노테이션 기반 설정

zsuling 2022. 9. 20. 09:49

<?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: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-4.3.xsd">

<context:component-scan base-package="polymorphism" />


</beans>

package polymorphism;

import org.springframework.stereotype.Component;

@Component("apple")
public class AppleSpeaker implements Speaker {
			
	AppleSpeaker(){
		System.out.println("AppleSpeaker 생성자 ");
	}
	
	@Override
	public void volumeUp() {
		System.out.println("==> AppleSpeaker volumeUp ");
		
	}

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

}

 


package polymorphism;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("SamSungTV")
public class SamSungTV implements TV{
	
	@Autowired
	@Qualifier("apple")
	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();
	}

}

package polymorphism;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("LgTV")
public class LgTV  implements  TV{
	/*
	@Autowired
	@Qualifier("sony")
	*/
	
	@Resource(name="sony")
	private Speaker speaker;
	
	private int  price =0;
	LgTV(){
		System.out.println("LgTV 생성자");
	}
	
	LgTV( Speaker speaker ){
		System.out.println("LgTV 생성자(1)");
		this.speaker = speaker;
	}
	LgTV( Speaker speaker, int  price ){
		System.out.println("LgTV 생성자(1)");
		this.speaker = speaker;
		this.price = price;
	}
		
	
	public void setSpeaker(Speaker speaker) {
		this.speaker = speaker;
		System.out.println("setSpeaker 호출");
	}

	public void setPrice(int price) {
		this.price = price;
		System.out.println("setPrice 호출");
		
	}

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

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

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

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

}
Comments