hyeonga_code

Spring_의존성 주입_스프링 의존성 관리 방법 본문

Spring

Spring_의존성 주입_스프링 의존성 관리 방법

hyeonga 2023. 12. 15. 08:59
반응형


- 스프링 의존성 관리 방법
      1. Dependency Lookup : 검색
      2. Dependency Injection : 주입
            - 의존성 설정을 바꾸고 싶은 경우 소스코드를 변경하지 않고 스프링 설정 파일만 수정하여 변경사항을 적용할 수 있어 유지보수가 향상됩니다.
            - Setter 메소드를 기반으로 하는 Setter Injection과 생성자를 기반으로 하는 Costructor Injection으로 나뉩니다.

- sts.spring.ioc2 패키지에 'SonySpeaker.java' 클래스 생성
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package sts.spring.ioc2;
 
public class SonySpeaker {
    public SonySpeaker() {
        System.out.println("+++   Create SonySpeaker  +++");
    }
    
    public void volumeUp() {
        System.out.println("+   SonySpeaker : volumeUp  +\n");
    }
 
    public void volumeDown() {
        System.out.println("+  SonySpeaker : volumeDown +\n");
    }
}
 


- sts.spring.ioc2 > 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
package sts.spring.ioc2;
 
public class SamsungTV implements TV {
    
    // SonySpeaker를 사용할 수 있게 추가합니다.
    private SonySpeaker sp;
 
    public SamsungTV() {
        System.out.println("[     Create SamsungTV      ]");
    }
    
    @Override
    public void powerOn() {
        System.out.println("SamsungTV powerOn\n");
    }
    
    @Override
    public void powerOff() {
        System.out.println("SamsungTV powerOff\n");
    }
    
    @Override
    public void volumeUp() {
        sp = new SonySpeaker();
        sp.volumeUp();
    }
    
    @Override
    public void volumeDown() {
        sp = new SonySpeaker();
        sp.volumeDown();
    }
}
 



- src/main/resources > applicationContext.xml 파일을 수정합니다.
=====

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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">
 
<!-- SonySpeaker 직접 사용     -->
    <bean id="samsungTV" class="sts.spring.ioc2.SamsungTV" />
 
</beans>



- sts.spring.ioc2 > 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
package sts.spring.ioc2;
 
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();
        
        // Spring 컨테이너를 종료합니다.
        fac.close();
            /*
                [     Create SamsungTV      ]
                SamsungTV powerOn
                
                +++   Create SonySpeaker  +++
                +   SonySpeaker : volumeUp  +
                
                +++   Create SonySpeaker  +++
                +  SonySpeaker : volumeDown +
                
                SamsungTV powerOff
             */
    }
}
 




































반응형