6.4.4 프록시 인터페이스

ProxyFactoryBean행동 의 간단한 예를 생각해보십시오 . 이 예제는 다음을 포함합니다.

  • 프록시되는 대상 bean. personTarget예제 의 bean 정의입니다.

  • advice를 제공하기 위해 advisor와 Interceptor 사용된다.

  • 대상 객체 ( personTargetbean), 프록시 인터페이스 및 적용 할 권고 사항을 지정하는 AOP 프록시 Bean 정의 .

다음 목록은 예제를 보여줍니다.

<bean id="personTarget" class="com.mycompany.PersonImpl">
    <property name="name" value="Tony"/>
    <property name="age" value="51"/>
</bean>

<bean id="myAdvisor" class="com.mycompany.MyAdvisor">
    <property name="someProperty" value="Custom string property value"/>
</bean>

<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor">
</bean>

<bean id="person"
    class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces" value="com.mycompany.Person"/>

    <property name="target" ref="personTarget"/>
    <property name="interceptorNames">
        <list>
            <value>myAdvisor</value>
            <value>debugInterceptor</value>
        </list>
    </property>
</bean>

위에서 interceptorNames에서 advisor, interceptor등의 배열을 가지고, 첫번째 인터셉터가 가로챈다고 하니까 debugInterceptor가 가로챌거야. person이라는 프록시 팩토리 빈은 Person 인터페이스를 확장하니까 Person인터페이스가 팩토리빈이 될거고 타겟은 PersonTarget 빈이야.

그러니까 Person 객체로 요청이 들어오면 debugInterceptor 인터셉터가 가로채서 myAdvisor가 조언하겠지?

interceptorNames프로퍼티는 String의 리스트를 취하는데 이는 현재 팩토리에 있는 인터셉터나 어드바이저의 빈 이름을 가지고있다. advisor, Interceptor 를 사용하기 전, 반환 한 후 advice 오브젝트를 던집니다. advisor의 순서는 중요합니다.

목록에 빈 참조가없는 이유가 궁금 할 것입니다. (list 받을 때 프로퍼티 명으로만 받고 ref를 받지 않은 것!!)그 이유는 ProxyFactoryBean의 singleton 속성이 false로 설정된 경우, 독립 프록시 인스턴스를 반환 할 수 있어야합니다. advisor 중 하나가 자체 프로토 타입 인 경우 독립 인스턴스를 리턴해야하므로 팩토리에서 프로토 타입의 인스턴스를 확보 할 수 있어야합니다. 참조를 보유하는 것만으로는 충분하지 않습니다.

person이전 같이 빈 정의는 대신 사용할 수 있습니다 Person다음과 같이 구현 :

Person person = (Person) factory.getBean("person");

동일한 IoC 컨텍스트에있는 다른 Bean은 일반적인 Java 오브젝트와 마찬가지로 강하게 유형화 된 종속성을 표현할 수 있습니다. 다음 예제에서는이를 수행하는 방법을 보여줍니다.

<bean id="personUser" class="com.mycompany.PersonUser">
    <property name="person"><ref bean="person"/></property>
</bean>

PersonUser이 예제 의 클래스는 Person type의 속성을 노출합니다. AOP 프록시는 "실제" Person 구현 대신 투명하게 사용할 수 있습니다. 그러나 이 클래스는 동적 프록시 클래스입니다. Advised인터페이스 로 전송할 수도 있습니다 (나중에 설명).

익명의 내부 빈을 사용하여 대상과 프록시를 구별 할 수 있습니다. ProxyFactoryBean정의 만 다릅니다. advice은 완전성을 위해서만 포함됩니다. 다음 예제에서는 익명 내부 bean을 사용하는 방법을 보여줍니다.

<bean id="myAdvisor" class="com.mycompany.MyAdvisor">
    <property name="someProperty" value="Custom string property value"/>
</bean>

<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>

<bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces" value="com.mycompany.Person"/>
    <!-- Use inner bean, not local reference to target -->
    <property name="target">
        <bean class="com.mycompany.PersonImpl"> //이부분이 익명의 내부 빈 
            <property name="name" value="Tony"/>
            <property name="age" value="51"/>
        </bean>
    </property>
    <property name="interceptorNames">
        <list>
            <value>myAdvisor</value>
            <value>debugInterceptor</value>
        </list>
    </property>
</bean>

익명의 내부 bean을 사용하면 Person유형이 하나뿐이라는 장점이 있습니다. 이것은 애플리케이션 컨텍스트의 사용자가 un-advised 객체에 대한 참조를 얻지 못하도록하거나 Spring IoC autowiring으로 모호한 것을 피할 필요가있을 때 유용합니다. 논쟁의 여지는 있지만 ProxyFactoryBean정의가 자급 자족 한다는 점에서 이점 이 있습니다. 그러나 팩토리에서 권고되지 않은 목표를 얻을 수있는 것이 실제로 이점이 될 수있는 경우가 있습니다 (예 : 특정 테스트 시나리오).

Last updated