Spring bean 상속


Spring Bean 상속


클래스 상속과는 개념이 다르다. AClass의 여러개의 property가 존재하고 setter injection으로 내부 property를 지정하였다고 가정하자. 이 때 단 한개의 property만 다른 새로운 bean을 정의해야 한다. AClass의 bean을 새로 정의하여 내부 property를 각각 지정해줘도 괜찮지만 반복작업이 발생하게 되고 한개의 property가 바뀌는 경우 두 bean의 정의를 모두 관리하여야 하는 단점이 있다. 이런 경우 사용하는 것이 bean상속이다.


bean상속은 parent=""로 지정한 bean의 기존 property는 유지하고 bean정의시 명시적으로 작성한 property만 새로운 value로 대체하여 준다.



용도

  • 같은 Class의 bean정의가 여러개 필요하고 개별 property의 value가 완전히 동일하지 않은 경우

사용방법

  • bean정의 시 parent="beanID"를 지정하여 부모 bean을 지정하여 준다.
  • parent로 지정된 bean과 비교하여 다른 property를 명시적으로 작성하여 준다.
1
2
3
4
5
6
7
    <!-- Class 상속과는 다르고, 같은 bean으로 상속받는 경우 parent bean의 일부 Property 변경이 가능 -->
    <bean id="childBean" class="com.spring.bean.SimpleClass" parent="parentBean">
        <property name="age">
            <value type="java.lang.Integer">50</value>
        </property>
    </bean>
 
cs



Example


[SimpleClass.java]

  • 별다른 내용이 없는 SimpleClass.
  • 내부 property로 name과 age가 있다.
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
package com.spring.bean;
 
public class SimpleClass {
    
    /* Properties */
    private String name;
    
    private int age;
    
    /* Getters and Setters */
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}
cs



[application-context.xml]

  • parent bean은 이지수, 1이 지정되고
  • child bean은 이지수, 50의 값이 저정되었다.
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
<?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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.2.xsd">
    
    <bean id="parentBean" class="com.spring.bean.SimpleClass">
        <property name="name">
            <value>이지수</value>
        </property>
        <property name="age">
            <value type="java.lang.Integer">1</value>
        </property>
    </bean>
    
    <!-- Class 상속과는 다르고, 같은 bean으로 상속받는 경우 parent bean의 일부 Property 변경이 가능 -->
    <bean id="childBean" class="com.spring.bean.SimpleClass" parent="parentBean">
        <property name="age">
            <value type="java.lang.Integer">50</value>
        </property>
    </bean>
    
</beans>
cs



[MainApplication.java]

  • parent bean과 child bean이 각각 어떤 property를 갖고 있는 지 확인해 본다.
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
package com.spring;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
import com.spring.bean.SimpleClass;
 
public class MainApplication {
 
    public static void main(String[] args) {
        // ApplicationContext 부트스트랩
        GenericXmlApplicationContext appCtx = new GenericXmlApplicationContext();
        appCtx.load("classpath:META-INF/spring/app-context.xml");
        appCtx.refresh();
        
        SimpleClass parent = appCtx.getBean("parentBean", SimpleClass.class);
        SimpleClass child = appCtx.getBean("childBean", SimpleClass.class);
        
        
        System.out.println(" Parent Bean info : ");
        printInfo(parent);
        
        System.out.println(" Child Bean info : ");
        printInfo(child);
    }
    
    public static void printInfo(SimpleClass bean) {
        System.out.println(" ************************* ");
        System.out.println(" name : "+bean.getName());
        System.out.println(" age : "+bean.getAge());
        System.out.println(" ************************* ");
    }
    
}
cs





수행결과

각각의 bean의 property가 name은 동일하게 지정되었지만, age는 다르게 지정된 것을 확인할 수 있다.

 Parent Bean info :
 *************************
 name : 이지수
 age : 1
 ************************* 


 Child Bean info :
 *************************
 name : 이지수
 age : 50
 *************************


첨부파일

SpringBeanParent.zip




이 글을 공유하기

댓글

Email by JB FACTORY