hyeonga_code

Spring_스프링 의존성 주입_생서자 인젝션 이용하기, 다중 변수 매핑 본문

Spring

Spring_스프링 의존성 주입_생서자 인젝션 이용하기, 다중 변수 매핑

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


- 생성자 인젝션 이용하기

- sts.spring.ioc2 > SamsungTV2.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
package sts.spring.ioc2;
 
public class SamsungTV2 implements TV {
    
    // SonySpeaker를 사용할 수 있게 추가합니다.
    private SonySpeaker sp;
 
    public SamsungTV2() {
        System.out.println("[     Create SamsungTV2     ]");
    }
    
    // 생성자 추가
    public SamsungTV2(SonySpeaker sp) {
        System.out.println("Create SamsungTV2 : Injection");
        this.sp = sp;
    }
 
    // getter/setter 메소드 추가
    public SonySpeaker getSpeaker() {
        return sp;
    }
    
    public void setSpeaker(SonySpeaker sp) {
        this.sp = sp;
    }
    
    
    @Override
    public void powerOn() {
        System.out.println("SamsungTV2 powerOn\n");
    }
    
    @Override
    public void powerOff() {
        System.out.println("SamsungTV2 powerOff\n");
    }
    
    @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
<?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">
 
<!-- Injection     -->
    <bean id="samsungTV2" class="sts.spring.ioc2.SamsungTV2">
        <constructor-arg ref="sony"></constructor-arg>
    </bean>
    <bean id="sony" class="sts.spring.ioc2.SonySpeaker" />
 
    
</beans>
 



- sts.spring.ioc2 > TVUser2.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
package sts.spring.ioc2;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class TVUser2 {
    public static void main(String[] args) {
        
        // Spring 컨테이너를 구동합니다.
        AbstractApplicationContext fac = new GenericXmlApplicationContext("applicationContext.xml");
        
        // Spring 컨테이너에 등록한 객체를 요청합니다.
        TV samsungTV2 = (TV) fac.getBean("samsungTV2");
        
        samsungTV2.powerOn();
        samsungTV2.volumeUp();
        samsungTV2.volumeDown();
        samsungTV2.powerOff();
        System.out.println();
        
        // Spring 컨테이너를 종료합니다.
        fac.close();
            /*
                +++   Create SonySpeaker  +++
                Create SamsungTV2 : Injection
                SamsungTV2 powerOn
                
                +   SonySpeaker : volumeUp  +
                
                +  SonySpeaker : volumeDown +
                
                SamsungTV2 powerOff
             */
    }
}
 




- 다중 변수 매핑하기
      - 생성자 인젝션에서 초기화해야 할 변수가 여러 개인 경우
- sts.spring.ioc2 > SamsungTV3.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.ioc2;
 
public class SamsungTV3 implements TV {
    
    private SonySpeaker sp;
    
    // 가격 변수를 선언합니다.
    private int price;
    
    public SamsungTV3() {
        System.out.println("[     Create SamsungTV3     ]");
    }
    
    public SamsungTV3(SonySpeaker sp) {
        System.out.println("Create SamsungTV3 : Injection");
        this.sp = sp;
    }    
    
    // 생성자 추가
    public SamsungTV3(SonySpeaker sp, int price) {
        System.out.println("Create SamsungTV3 : MultiMapping Injection");
        this.sp = sp;
        this.price = price;
    }
 
    // getter/setter 메소드 추가
    public SonySpeaker getSpeaker() {
        return sp;
    }
    
    public void setSpeaker(SonySpeaker sp) {
        this.sp = sp;
    }
    
    
    @Override
    public void powerOn() {
        System.out.println("SamsungTV3 powerOn\n   - price : " + price + "/n");
    }
    
    @Override
    public void powerOff() {
        System.out.println("SamsungTV3 powerOff\n");
    }
    
    @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
<?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">
 
<!-- Injection : Multi     -->
    <bean id="samsungTV3" class="sts.spring.ioc2.SamsungTV3">
        <constructor-arg ref="sony" index="0"></constructor-arg>
        <constructor-arg value="15000" index="1"></constructor-arg>
    </bean>
    <bean id="sony" class="sts.spring.ioc2.SonySpeaker" />
 
</beans>
 



- sts.spring.ioc2 > TVUser3.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
package sts.spring.ioc2;
 
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
 
public class TVUser3 {
    public static void main(String[] args) {
        
        // Spring 컨테이너를 구동합니다.
        AbstractApplicationContext fac = new GenericXmlApplicationContext("applicationContext.xml");
        
        // Spring 컨테이너에 등록한 객체를 요청합니다.
        TV samsungTV3 = (TV) fac.getBean("samsungTV3");
        
        samsungTV3.powerOn();
        samsungTV3.volumeUp();
        samsungTV3.volumeDown();
        samsungTV3.powerOff();
        System.out.println();
        
        // Spring 컨테이너를 종료합니다.
        fac.close();
            /*
                +++   Create SonySpeaker  +++
                Create SamsungTV3 : MultiMapping Injection
                SamsungTV3 powerOn
                   - price : 15000/n
                +   SonySpeaker : volumeUp  +
                
                +  SonySpeaker : volumeDown +
                
                SamsungTV3 powerOff
 
             */
    }
}
 




























반응형