반응형

목차


beans

dispatcher-servlet.xml 혹은 applicationContext.xml 의 루트태그. 태그 속성으로 스프링 bean이 사용할 라이브러리를 정의한다. 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-3.1.xsd
            http://www.springframework.org/schema/task 
            http://www.springframework.org/schema/task/spring-task-3.1.xsd">  
</beans>
cs

<beans>의 속성값은 실제 참조하는 스프링 라이브러리의 버전에 따라 달라진다. 위에선 spring 3.1을 사용하는 경우다.


bean

객체 생성 태그

<bean id="아이디" class="클래스"/>

  • id: 스프링 bean이 인식하는 식별자
  • class: 객체를 생성할 클래스
package com.bean;
 
public class User {
    String str;
    public User() {
        this.str = "ㅎㅇ";
    }
    
    public String result() {
        return str;
    }
}
cs

위와 같은 클래스가 있다고 할 때 applicationContext.xml 에 다음을 추가 한 뒤

<bean id="myBean" class="com.temp.User"/>
cs

실행한다:

AbstractApplicationContext context 
          = new ClassPathXmlApplicationContext("com/bean/applicationContext.xml");
User us = (User)context.getBean("myBean"); //bean이 생성한 객체는 Object 타입
System.out.println(us.result()); // "ㅎㅇ"
cs


constructor-arg

생성자의 가변인자에 값을 할당한다.

<bean id="아이디" class="클래스">

<constructor-arg value="값"/>

<constructor-arg ref="참조값"/>

</bean>

value: 원시형 데이터

ref: 참조형 데이터(bean으로 생성한 객체의 아이디)

package com.bean;
 
public class User1 {
    String str, str2;
    
    public User1(String str, String str2) {
        this.str = str;
        this.str2 = str2;
    }
    
    public String result() {
        return str + " " + str2;
    }
}
cs

위와 같은 클래스가 있을 때 다음처럼 태그 작성 후

<bean id="user1" class="com.bean.User1">
    <constructor-arg value="으앙"/<!--value : 기본자료형 데이터-->
    <constructor-arg value="쥬금"/>  <!--순서에 주의할 것-->
</bean>
cs

아래처럼 실행한다:

AbstractApplicationContext context
          = new ClassPathXmlApplicationContext("com/bean/applicationContext.xml");
User1 us1 = (User1)context.getBean("user1");
System.out.println(us1.result());
cs

생성자에 참조타입을 할당하는 것도 가능하다.

<bean id="user1" class="com.sp1.UserImpl"/>
<bean id="testService1" class="com.sp1.TestService">
    <constructor-arg ref="user1"/<!-- ref : 참조형 변수를 뜻함 -->
</bean>
cs


property

필드, 클래스 변수나 인스턴스 변수를 의미하며 setter 메서드를 이용해 값을 할당한다. 따라서 setter 메서드가 없으면 사용할 수 없다.

Syntex 1: body가 있는 경우

<bean id="아이디" class="클래스">

    <property name="변수명">

        <value type="클래스_타입">값</value>

        <ref bean="참조값"/>

    </property>

</bean>


Syntex 2: body가 없는 경우

<bean id="아이디" class="클래스">

    <property name="변수명" value="값"/>

    <property name="변수명" ref="참조값"/>

</bean>

<!--Syntex 1-->
<bean id="user1" class="com.sp2.UserImpl">
    <property name="name">
        <value type="java.lang.String">스프링</value
    </property>
 
    <property name="tel">
        <value type="java.lang.Integer">20</value
    </property>
</bean>
 
<!--Syntex 2-->
<bean id="user1" class="com.sp2.UserImpl">
    <property name="name" value="어렵네"/>
    <property name="age" value="30"/>
</bean>
cs

위는 자바에서 다음처럼 작성한 것과 같음: 

UserImpl user1 = new UserImpl();
user.setName("스프링");
user.setAge("20");
cs

참조형 데이터의 경우엔: 

<!--Syntex 1-->
<bean id="us" class="com.sp2.UserService">
    <property name="user">
        <ref bean="ob"/>
    </property>    
</bean>
 
<!--Syntex 2-->
<bean id="userService" class="com.sp2.UserService">
    <property name="user" ref="user1"/>
</bean>
cs
UserService us = new UserService();
user.setUser(ob);
cs


p 네임스페이스

기존의 구문을 축약하여 작성할 수 있게 해준다.

<bean id="아이디" class="클래스" p:변수명="값" p:변수명-ref="참조값"/>

<!-- p 네임스페이스를 이용한 프로퍼티 설정 -->
<bean id="user2" class="com.sp2.UserImpl" p:name="자바" p:tel="010-1111-1111"/>
 
<!-- p 네임스페이스를 이용한 의존관계설정 -->
<bean id="userService2" class="com.sp2.UserService" p:user-ref="user2"/>
cs


list

ArrayList에 대응된다.

<bean id="아이디" class="클래스">

    <property name="변수명">

        <list>

            <value>첫번째 값</value>

            <value>두번째 값</value>

            ...

        </list>

    </property>

</bean>

<bean id="sample" class="com.test.SampleClass">
    <property name="hobby">
        <list>
            <value>소개팅</value>
            <value>운동</value>
            <value>잠자기</value>
        </list>
    </property>
</bean>
cs


map

HashMap에 대응

Syntex 1:

<bean id="아이디" class="클래스">

    <property name="address">

        <map>

            <entry>

                <key><value>키</value></key>

                <value>벨류</value>

            </entry>

        </map>

    </property>

</bean>


Syntex 2:

<bean id="아이디" class="클래스">

    <property name="address">

        <map>

            <entry key="값_키" value="값_벨류"/>

            <entry key-ref="참조값_키" value-ref="참조값_벨류"/>

        </map>

    </property>

</bean>

<!--Syntex 1-->
<bean id="user" class="com.sp3.UserImpl">
    <property name="address">
        <map>
            <entry>
                <key><value>이상해</value></key>
                <value>서울</value<!-- key="이상해", value="서울" -->
            </entry>
        </map>
    </property>
</bean>
 
<!--Syntex 2-->
<bean id="user" class="com.sp3.UserImpl">
    <property name="address">
        <map>                
            <entry key="이이이" value="부산"/<!-- key="이이이", value="부산" -->
        </map>
    </property>
</bean>
 
<!--Syntex 3-->
<bean id="user" class="com.sp3.UserImpl">
    <property name="address">
        <map>
            <entry key-ref="객체" value-ref="객체2"/<!-- key=객체 value=객체2 -->
        </map>
    </property>
</bean>
cs


props

properties에 대응되며 문법은 map과 같다.

<bean id="handlerMapping" class="servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
         <props>
             <prop key="/bbs/*.action">bbs.boardController</prop>
         </props>
     </property>
</bean>
cs

출처 : http://noritersand.tistory.com/152

+ Recent posts