1HOON
논리적 코딩
1HOON
전체 방문자
오늘
어제
  • HOME (187) N
    • ☕️ JAVA (28)
      • WhiteShip Java LIVE Study (6)
      • Effective JAVA (10)
    • 🔮 KOTLIN (4)
    • 🌱 SPRING (51)
      • 스프링 인 액션 (22)
      • JPA (18)
    • ☕️ JAVASCRIPT (6)
    • 📃 DATABASE (40)
      • ORACLE (37)
      • MSSQL (2)
    • 🐧 LINUX (4)
    • 🐳 DOCKER (5)
    • 🐙 KUBERNETES (4)
    • 🏗️ ARCHITECTURE (8)
    • 📦 ETC (27) N
      • TOY PROJECT (5)
      • RECRUIT (1)
      • 그냥 쓰고 싶어서요 (14)
      • TIL (1) N
    • 🤿 DEEP DIVE (1)
    • 🚽 Deprecated (9)
      • PYTHON (3)
      • AWS (2)
      • HTTP 완벽가이드 (3)
      • WEB (1)

블로그 메뉴

  • 홈
  • 방명록
  • 관리

인기 글

최근 글

티스토리

hELLO · Designed By 정상우.
1HOON

논리적 코딩

XML로 빈 와이어링하기
🌱 SPRING/스프링 인 액션

XML로 빈 와이어링하기

2018. 5. 7. 15:50

XML로 빈 와이어링하기

1. XML 설정 스펙 만들기

JavaConfig 에서 @Configuration 어노테이트된 클래스를 만들듯이 루트 XML 파일을 만든다.


1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmls="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
        http://www.springframework.org/schema/context">
 
</beans>
Colored by Color Scripter
cs


2. 간단한 빈 선언

<bean> 요소를 사용해 빈을 선언한다. JavaConfig 의 @Bean과 유사하다고 보면된다.


1
<bean class="com.test.logicalCode.soundSystem.SgtPeppers" />
cs


ID 값을 별도로 설정하지 않았기 때문에 이 빈의 ID는 com.test.logicalCode.soundSystem.SgtPeppers#0 이다.

하지만 사용자를 위해 자동으로 빈 ID가 명명되어도 자동으로 만들어지는 이름은 유용성이 떨어지므로 ID를 부여하도록한다.


1
<bean id="compactDisc" class="com.test.logicalCode.soundSystem.SgtPeppers" />
cs


3. 생성자 주입을 사용하여 빈 초기화하기

XML로 DI를 선언할 때, 여러 옵션이 있는데, 특정 생성자 주입과 관련해서는 두 개의 기본 옵션이 있다.


- <contstructor-arg> 요소

- 스프링 3.0에서 도입된 c-네임스페이스


빈 레퍼런스를 사용한 생성자 주입

1. <contstructor-arg> 요소를 이용한 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="UTF-8"?>
<beans xmls="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
        http://www.springframework.org/schema/context">
            
    <bean id="compactDisc" class="com.test.logicalCode.soundSystem.SgtPeppers" />
                
    <bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer">
        <contstructor-arg ref="compactDisc" />
    </bean>
    
</beans>
Colored by Color Scripter
cs


2. c-namespace 를 이용한 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmls="http://www.springframework.org/schema/beans"
    xmlns:c="http://www.springframework.org/schema/c"
    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
        http://www.springframework.org/schema/context">
            
    <bean id="compactDisc" class="com.test.logicalCode.soundSystem.SgtPeppers" />
    
    <bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer" c:cd-ref="compactDisc" />
        
</beans>
Colored by Color Scripter
cs


리터럴 값을 생성자에 주입하기

생성자의 매개변수로 String 값 두 개를 받는다고 가정을 하면 아래와 같이 사용할 수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="UTF-8"?>
<beans xmls="http://www.springframework.org/schema/beans"
    xmlns:c="http://www.springframework.org/schema/c"
    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
        http://www.springframework.org/schema/context">
        
    <bean id="compactDisc" class="com.test.logicalCode.soundSystem.SgtPeppers" >
        <constructor-arg value="Let it be" />
        <constructor-arg value="Beatles" />
    </bean>
    
    <bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer" c:cd-ref="compactDisc" />
            
</beans>
Colored by Color Scripter
cs


와이어링 컬렉션

그렇다면 컬렉션을 매개변수로 넘기게되면 어떻게 표현해야할까?


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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmls="http://www.springframework.org/schema/beans"
    xmlns:c="http://www.springframework.org/schema/c"
    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
        http://www.springframework.org/schema/context">
        
    <bean id="compactDisc" class="com.test.logicalCode.soundSystem.SgtPeppers" >
        <constructor-arg value="Let it be" />
        <constructor-arg value="Beatles" />
        <constructor-arg>
            <list>
                <value>Two of us</value>
                <value>Dig a Pony</value>
                <value>Across the Universe</value>
                <value>I Me Mine</value>
                <value>Dig it</value>
                <value>Let it Be</value>
                <value>Maggie Mae</value>
                <value>I'vet got a Feeling</value>
                <value>One after 909</value>
                <value>The long and winding road</value>
                <value>For you blue</value>
                <value>Get back</value>
            </list>
        </constructor-arg>
    </bean>
            
    <bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer" c:cd-ref="compactDisc" />
            
</beans>
Colored by Color Scripter
cs


<list>에는 빈 또한 올 수 있다.


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 xmls="http://www.springframework.org/schema/beans"
    xmlns:c="http://www.springframework.org/schema/c"
    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
        http://www.springframework.org/schema/context">
        
    <bean id="compactDisc" class="com.test.logicalCode.soundSystem.SgtPeppers" >
        <constructor-arg value="Let it be" />
        <constructor-arg value="Beatles" />
        <constructor-arg>
            <list>
                <ref bean="sgtPeppers">
                <ref bean="whiteAlbum">
            </list>
        </constructor-arg>
    </bean>
            
    <bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer" c:cd-ref="compactDisc" />
            
</beans>
Colored by Color Scripter
cs


4. 프로퍼티 세팅

기본 생성자 외에 다른 생성자를 가지지 않은 클래스는 다음과 같이 빈으로 만들 수 있다.


1
<bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer" />
cs


그런데 이 빈의 프로퍼티를 세팅해주고 싶을 땐 어떻게 해야할까? 별도 생성자가 없으니 3번과 같은 방법은 사용할 수 없다.


1
2
3
<bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer" >
    <property name="compactDisc" ref="compactDisc" />
</bean>
Colored by Color Scripter
cs


이렇게 <property> 요소를 이용하면 cdPlayer의 프로퍼티 compactDisc는 compactDisc 빈을 참조해 이용한다.

또한, <property>는 p-네임스페이스로 대체 가능한데, 방법은 아래와 같다.


1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<beans xmls="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    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
        http://www.springframework.org/schema/context">
    
    <bean id="compactDisc" class="com.test.logicalCode.soundSystem.SgtPeppers" />
    
    <bean id="cdPlayer" class="com.test.logicalCode.soundSystem.CDPlayer" p:compactDisc-ref="compactDisc" />
        
</beans>
Colored by Color Scripter
cs


 


 이 포스트는 스프링 인 액션을 읽고 개인적으로 필요하다고 생각되는 내용을 정리한 포스트입니다.

일부 내용, 소스코드는 스프링 인 액션의 내용일 수 있습니다.

반응형
저작자표시 비영리 변경금지 (새창열림)

'🌱 SPRING > 스프링 인 액션' 카테고리의 다른 글

AOP란?  (0) 2018.05.24
조건부 빈  (0) 2018.05.21
자바로 빈 와이어링하기  (0) 2018.05.07
빈 와이어링 - 자동 와이어링  (0) 2018.05.07
스프링 컨테이너  (0) 2018.05.06
    '🌱 SPRING/스프링 인 액션' 카테고리의 다른 글
    • AOP란?
    • 조건부 빈
    • 자바로 빈 와이어링하기
    • 빈 와이어링 - 자동 와이어링
    1HOON
    1HOON

    티스토리툴바