728x90


저번에 생성자 인젝션을 배웠는데 이번에는 설정자 인젝션을 배워보자.

특정 필드(프로퍼티)값을 추가하는 방식이 꼭 생성자만 있는것은 아니라는것을 여러분은 알것이다.

따라서 spring에서는 설정자 인젝션 역시 제공하고 있다.

그럼 과거 xml파일을 한번 다시 보자.


<?xml version="1.0" encoding="UTF-8"?>

<!-- applicationContext.xml -->

<beans xmlns="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">

    

    <bean id="warrior" class="maple.Warrior">

    <constructor-arg value="500"></constructor-arg>

    <constructor-arg value="300"></constructor-arg>

    <constructor-arg ref="sword"></constructor-arg>

    <property name="hp" value="700"></property>

    </bean>

    <bean id="mage" class="maple.Mage">

    <constructor-arg value="300"></constructor-arg>

    <constructor-arg value="500"></constructor-arg>

    <constructor-arg ref="wand"></constructor-arg>

    </bean>

    

    <bean id="wand" class="maple.Wand">

    <constructor-arg value="아케인셰이드 완드"></constructor-arg>

    </bean>

    <bean id="sword" class="maple.Sword">

    <constructor-arg value="아케인셰이드 세이버"></constructor-arg>

    </bean>

</beans>


여기서 우리는 constructor-arg를 사용해서 값을 대입했었다.

그러나 warrior의 밑을 유심히보면 property라는 것이 있다. 이 녀석은 여러분이 만든 생성자로 값을 대입하는 것이다.

자바는 설정자라는 문법이 존재하지 않는다. 그냥 다 암묵적 규칙으로 생성자라는 애들을 만들어 놓고 쓰는 것이다.

그러면 자바에서는 설정자를 어떻게 인식하게 될까?

자바에서 메소드는 일반적으로 카멜 캐이싱으로 두문은 소문자로, 단락은 대문자로 구별한다.

일단 설정자 역시 자바에서는 일반 메소드이므로 카멜 캐이싱을 지킨다.

예를들어 hp라는 필드를 설정자를 만들경우 setHp같은 형태로 만들게 된다.

스프링은 이걸 구별한다. 즉 setHp같은 형태를 설정자라 판단한다.

우리가 이 설정자를 사용하고 싶으면 set을 떼고 두문을 소문자로 바꾼 값, 즉 필드를 사용하게 된다는 것이다.

사용방법은 constructor-arg를 사용할때와 동일하다. 일반 값의 경우 value를, 참조 값의 경우 ref를 사용해주면 된다.

<?xml version="1.0" encoding="UTF-8"?>

<!-- applicationContext.xml -->

<beans xmlns="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">

    

    <bean id="warrior" class="maple.Warrior">

    <constructor-arg value="500"></constructor-arg>

    <constructor-arg value="300"></constructor-arg>

    <constructor-arg ref="sword"></constructor-arg>

    <property name="hp" value="700"></property>

    <property name="weapon" ref="swordTrash"></property>

    </bean>

    <bean id="mage" class="maple.Mage">

    <constructor-arg value="300"></constructor-arg>

    <constructor-arg value="500"></constructor-arg>

    <constructor-arg ref="wand"></constructor-arg>

    </bean>

    

    

    <bean id="wand" class="maple.Wand">

    <constructor-arg value="아케인셰이드 완드"></constructor-arg>

    </bean>

    <bean id="sword" class="maple.Sword">

    <constructor-arg value="아케인셰이드 세이버"></constructor-arg>

    </bean>

    <bean id="swordTrash" class="maple.Sword">

    <constructor-arg value="몽둥이"></constructor-arg>

    </bean>

</beans>


별 문제 없이 실행되는걸 확인할 수 있다.

설정자 인젝션은 더욱 진보한 방식을 사용할 수 있는게 그것은 p네임스페이스이다.

p네임스페이스를 보자.


p 네임스페이스



p 네임스페이스를 선택하려면 먼저 아래 탭에서 Namespaces를 눌러준다.

물론 gui환경이 아니라 그냥 타이핑해도 되지만 이왕 있는 기능을 사용하자.



그러면 목록들이 나오는데 그 중에서 p를 선택해준다.


그러면 이렇게 xmlns:p가 선택된다. 타이핑을 하자면 아래와 같다.


xmlns:p="http://www.springframework.org/schema/p"


이녀석으로 쉽게 쓸수 있다. 따라서 해보라.


<?xml version="1.0" encoding="UTF-8"?>

<!-- applicationContext.xml -->

<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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    

    <bean id="warrior" class="maple.Warrior" p:hp="436" p:mp="120" p:weapon-ref="swordTrash"/>

    <bean id="mage" class="maple.Mage">

    <constructor-arg value="300"></constructor-arg>

    <constructor-arg value="500"></constructor-arg>

    <constructor-arg ref="wand"></constructor-arg>

    </bean>

    

    

    <bean id="wand" class="maple.Wand">

    <constructor-arg value="아케인셰이드 완드"></constructor-arg>

    </bean>

    <bean id="sword" class="maple.Sword">

    <constructor-arg value="아케인셰이드 세이버"></constructor-arg>

    </bean>

    <bean id="swordTrash" class="maple.Sword">

    <constructor-arg value="몽둥이"></constructor-arg>

    </bean>

</beans>


쓰는 방법역시 비교적 간단해졌다. 특정 엘레멘트의 속성으로 p:를 쓴후 해당 필드를 선언해주면된다.

만약사용하려는게 참주변수라면 접미사로 -ref를 붙혀준다.

p 네임 스페이스역시 설정자의 연장선이다. 아무래도 xml은 타이핑 수가 많다.

따라서 설정자로 인젝션을 사용할 경우 p네임스페이슬 사용해주는게 유리하다.

+ Recent posts