Spring facoty-bean, factory-method


factory-bean / factory-method


앞에 진행했던 FactoryBean Interface 예제와 개념 및 용도 모든게 동일하지만, 구현 방법이 조금 다르다. 별도의 Interface를 구현할 필요가 업고 bean정의시 factory-bean과 factory-method를 지정하여 주면 이전에 예제와 동일한 방식으로 동작하게 된다. 본 포스트는 상세 설명 말고 이전 포스트와 차이점을 위주로 설명한다. 


작성 방법이 조금 다를 뿐 결과는 동일하다. 책에서는 Factory interface를 상속받아 구현할 수 없는 경우. 즉 Factory Class자체가 서드파티에 존재하여 내가 직접 수정하기 불가능 한 경우에 우회 방법으로 쓰는 걸 이야기한다. (근데 책이 너무 어렵게 설명을 해놔서 맞게 이해를 한건지..)


여튼 변경 방법 자체는 간단하다.


Example

 

[MessageFactory Class]

Interface를 제거하여 불필요한 메소드는 삭제한 뒤 getBean() 메소드 명을 getInstance()로 변경하였다.

getInstance() 메소드가 나중에 factory-method 로 지정된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.spring.factory;
 
import com.mesage.provider.HelloMessageProvider;
import com.mesage.provider.HiMessageProvider;
import com.mesage.provider.IMessageProvider;
 
public class MessageFactoryBean {
 
    private String type;
    
    public IMessageProvider getInstance() {
        
        if("Hi".equals(type))
            return HiMessageProvider.getInstance();
        
        else 
            return HelloMessageProvider.getInstance();
    }
 
 
    public void setType(String type) {
        this.type = type;
    }
}
cs

 

[application-context.xml]

hiProvider bean과 helloProvider bean을 추가작성 하였고, factory-bean, factory-method를 지정하였다. (24~26line)

실제 renderrer에 주입되는 건 hiProvider와, helloProvider bean 이다. (이전에는 hiFactoryBean, helloFacoryBean이 주입되었다)

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
38
39
40
41
42
<?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">
    
    <!-- FacoryBean 정의 -->
    <bean id="hiFactoryBean" class="com.spring.factory.MessageFactoryBean">
        <property name="type">
            <value>Hi</value>
        </property>
    </bean>
    
    <!-- FacoryBean 정의 -->
    <bean id="helloFactoryBean" class="com.spring.factory.MessageFactoryBean">
        <property name="type">
            <value>Hello</value>
        </property>
    </bean>
    
    <!-- bean을 정의하고 factory-bean, factory-method 지정 -->
    <bean id="hiProvider" factory-bean="hiFactoryBean"         factory-method="getInstance"/>
    
    <bean id="helloProvider" factory-bean="helloFactoryBean" factory-method="getInstance"/>
    
    
    <!-- 위에 적의된 FactoryBean을 주입을 통해 사용 -->
    <bean id="messageRenderrer" class="com.message.render.MessageRederrer">
        <property name="hiProvider">
            <ref bean="hiProvider"/>
        </property>
        
        <property name="helloProvider">
            <ref bean="helloProvider"/>
        </property>
    </bean>
    
</beans>
 
 
cs


[MainApplication Class]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.spring;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
import com.mesage.provider.IMessageProvider;
import com.message.render.MessageRederrer;
import com.spring.factory.MessageFactoryBean;
 
public class MainApplication {
 
    public static void main(String[] args) {
        
        GenericXmlApplicationContext appCtx = new GenericXmlApplicationContext();
        appCtx.load("classpath:META-INF/spring/app-context.xml");
        appCtx.registerShutdownHook();
        appCtx.refresh();
        
        MessageRederrer render = (MessageRederrer )appCtx.getBean("messageRenderrer");
        render.printMessage();
        
        appCtx.close();
    }
}
 
cs


수행결과

HI world

Hello World



다운로드

SpringFactoryMethod.zip



'프로그래밍 > Spring FWK' 카테고리의 다른 글

Spring ApplicationEvent  (0) 2016.01.10
Spring MessageSource 국제화  (0) 2016.01.08
Spring CustomPropertyEditor  (2) 2016.01.05
Spring PropertyEditor  (0) 2016.01.05
Spring FactoryBean Interface  (0) 2015.12.29
Spring ApplicationContextAware Interface  (0) 2015.12.28
Spring BeanNameAware Interface  (1) 2015.12.28
Spring Bean Life Cycle (빈 생명주기 관리)  (0) 2015.12.28

이 글을 공유하기

댓글

Email by JB FACTORY