hyeonga_code

Spring_의존성 주입_의존 관계의 변경 본문

Spring

Spring_의존성 주입_의존 관계의 변경

hyeonga 2023. 12. 16. 06:59
반응형


- sts.spring.ioc3 패키지 생성하기
- sts.spring.ioc3 > 'Speaker.java' 클래스 생성
=====

1
2
3
4
5
6
7
package sts.spring.ioc3;
 
public interface Speaker {
    void volumeUp();
    void volumeDown();
}
 




- sts.spring.ioc3 > 'AppleSpeaker.java' 클래스 생성
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sts.spring.ioc3;
 
public class AppleSpeaker implements Speaker {
    
    public AppleSpeaker() {
        System.out.println("+++  Create AppleSpeaker  +++");
    }
    
    @Override
    public void volumeUp() {
        System.out.println("+  AppleSpeaker : volumeUp  +");
    }
 
    @Override
    public void volumeDown() {
        System.out.println("+ AppleSpeaker : volumeDown +");
    }
}
 



- sts.spring.ioc3 > 'SonySpeaker.java' 클래스 생성
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sts.spring.ioc3;
 
public class SonySpeaker implements Speaker {
    
    public SonySpeaker() {
        System.out.println("+++   Create SonySpeaker  +++");
    }
    
    @Override
    public void volumeUp() {
        System.out.println("+   SonySpeaker : volumeUp  +");
    }
 
    @Override
    public void volumeDown() {
        System.out.println("+  SonySpeaker : volumeDown +");
    }
}
 


- sts.spring.ioc3 > SamsungTV.java 클래스 생성
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package sts.spring.ioc3;
 
public class SamsungTV implements TV {
    
    private Speaker sp;
    
    // 가격 변수를 선언합니다.
    private int price;
    
    public SamsungTV() {
        System.out.println("[     Create SamsungTV     ]");
    }
    
    public SamsungTV(Speaker sp) {
        System.out.println("Create SamsungTV : Injection");
        this.sp = sp;
    }    
    
    // 생성자 추가
    public SamsungTV(Speaker sp, int price) {
        System.out.println("Create SamsungTV : MultiMapping Injection");
        this.sp = sp;
        this.price = price;
    }
 
    // getter/setter 메소드 추가
    public Speaker getSpeaker() {
        return sp;
    }
    
    public void setSpeaker(Speaker sp) {
        this.sp = sp;
    }
    
    
    @Override
    public void powerOn() {
        System.out.println("SamsungTV powerOn\n   - price : " + price + "\n");
    }
    
    @Override
    public void powerOff() {
        System.out.println("SamsungTV powerOff");
    }
    
    @Override
    public void volumeUp() {
        sp.volumeUp();
    }
    
    @Override
    public void volumeDown() {
        sp.volumeDown();
    }
}
 


- src/main/resources > applicationContext.xml
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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:c="http://www.springframework.org/schema/c"
    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">
 
<!-- 의존관계 변경 -->
    <bean id="samsungTV" class="sts.spring.ioc3.SamsungTV">
        <constructor-arg index="0" ref="apple"></constructor-arg>
        <constructor-arg index="1" value="25000"></constructor-arg>
    </bean>
    <bean id="lgTV" class="sts.spring.ioc3.LgTV">
        <constructor-arg index="0" ref="sony"></constructor-arg>
        <constructor-arg index="1" value="12000"></constructor-arg>
    </bean>
    <bean id="sony" class="sts.spring.ioc3.SonySpeaker" />
    <bean id="apple" class="sts.spring.ioc3.AppleSpeaker" />
 
</beans>




- sts.spring.ioc3 > LgTV.java 클래스 생성
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package sts.spring.ioc3;
 
public class LgTV implements TV{
 
    private Speaker sp;
    
    // 가격 변수를 선언합니다.
    private int price;
    
    public LgTV() {
        System.out.println("[     Create LgTV     ]");
    }
    
    public LgTV(Speaker sp) {
        System.out.println("Create LgTV : Injection");
        this.sp = sp;
    }    
    
    // 생성자 추가
    public LgTV(Speaker sp, int price) {
        System.out.println("Create LgTV : MultiMapping Injection");
        this.sp = sp;
        this.price = price;
    }
 
    // getter/setter 메소드 추가
    public Speaker getSpeaker() {
        return sp;
    }
    
    public void setSpeaker(Speaker sp) {
        this.sp = sp;
    }
    
    
    @Override
    public void powerOn() {
        System.out.println("LgTV powerOn\n   - price : " + price + "\n");
    }
    
    @Override
    public void powerOff() {
        System.out.println("LgTV powerOff");
    }
    
    @Override
    public void volumeUp() {
        sp.volumeUp();
    }
    
    @Override
    public void volumeDown() {
        sp.volumeDown();
    }
}
 



- sts.spring.ioc3 > TVUser.java 클래스 생성
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package sts.spring.ioc3;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class TVUser {
    public static void main(String[] args) {
        
        // Spring 컨테이너를 구동합니다.
        AbstractApplicationContext fac = new GenericXmlApplicationContext("applicationContext.xml");
        
        // Spring 컨테이너에 등록한 객체를 요청합니다.
        TV samsungTV = (TV) fac.getBean("samsungTV");
        
        samsungTV.powerOn();
        samsungTV.volumeUp();
        samsungTV.volumeDown();
        samsungTV.powerOff();
        System.out.println();
        
        TV lgTV = (TV) fac.getBean("lgTV");
        
        lgTV.powerOn();
        lgTV.volumeUp();
        lgTV.volumeDown();
        lgTV.powerOff();
        
        // Spring 컨테이너를 종료합니다.
        fac.close();
            /*
                +++  Create AppleSpeaker  +++
                Create SamsungTV : MultiMapping Injection
                +++   Create SonySpeaker  +++
                Create LgTV : MultiMapping Injection
                SamsungTV powerOn
                   - price : 25000
                
                +  AppleSpeaker : volumeUp  +
                + AppleSpeaker : volumeDown +
                SamsungTV powerOff
                
                LgTV powerOn
                   - price : 12000
                
                +   SonySpeaker : volumeUp  +
                +  SonySpeaker : volumeDown +
                LgTV powerOff
             */
    }
}
 




































반응형