EJB 3.x 인터페이스

인터셉터는 EJB(Enterprise JavaBeans)의 비즈니스 메소드가 호출되거나 EJB의 라이프사이클 이벤트가 발생하면 자동으로 호출되는 메소드입니다.

세 가지 유형의 인터셉터 메소드가 있습니다. 비즈니스 메소드 인터셉터, 제한시간 메소드 인터셉터(EJB3.1에서 새로운 기능) 및 라이프사이클 콜백 인터셉터입니다. 비즈니스 메소드 인터셉터는 비즈니스 메소드에 대한 호출 주변에서 호출됩니다. 제한시간 메소드 인터셉터는 EJB 제한시간 메소드에 대한 호출 주변에서 호출됩니다. 라이프사이클 콜백 인터셉터는 PostConstruct, PreDestroy, PrePassivate 또는 PostActivate 라이프사이클 이벤트 중 하나의 주변에서 호출됩니다. 각 인터셉터 유형의 경우, 개별 클래스는 하나의 인터셉터 메소드만을 선언할 수 있습니다. 그러나 클래스 계층의 각 클래스는 각 인터셉터 유형에 대해 인터셉터 메소드를 선언할 수 있습니다. 서브클래스의 인터셉터 메소드가 수퍼클래스의 동일한 메소드를 대체하면, 서브클래스의 메소드만이 호출될 수 있습니다.

인터셉터 메소드는 연관된 메소드가 호출하도록 허용된 모든 자원과 컴포넌트에 액세스하고 호출할 수 있습니다. 또한, 인터셉터 메소드는 연관된 메소드와 동일한 트랜잭션과 보안 컨텍스트로 실행합니다. 싱글톤 세션 Bean을 제외하고, 라이프사이클 인터셉터 메소드는 LTC(Local Transaction Containment)로 실행됩니다.

EJB 클래스나 별도의 인터셉터 클래스에서 직접 인터셉터 메소드를 선언할 수 있습니다. 별도의 클래스에서 인터셉터 메소드를 선언하려면, 어노테이션 또는 XML을 사용하여 인터셉터 클래스를 EJB에 바인드해야 합니다. 다음 예를 사용하여 어노테이션을 사용하여 인터셉터를 선언합니다.

@Interceptors({ClassInterceptor1.class, ClassInterceptor2.class})
public class TestBean { /* ... */ }

@Interceptors({ClassInterceptor1.class})
public class TestBean2 {
	@Interceptors({MethodInterceptor1.class, MethodInterceptor2.class})
	public void businessMethod() { /* ... */ }
}

다음 예를 사용하여, 배치 디스크립터를 사용하여 인터셉터를 선언합니다.

  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>TestBean</ejb-name>
      <interceptor-class>ClassInterceptor1</interceptor-class>
      <interceptor-class>ClassInterceptor2</interceptor-class>
    </interceptor-binding>

    <interceptor-binding>
      <ejb-name>TestBean2</ejb-name>
      <interceptor-class>ClassInterceptor1</interceptor-class>
    </interceptor-binding>

    <interceptor-binding>
      <ejb-name>TestBean2</ejb-name>
      <interceptor-class>MethodInterceptor1</interceptor-class>
      <interceptor-class>MethodInterceptor2</interceptor-class>
           <method>
        <method-name>businessMethod</method-name>
           </method>
    </interceptor-binding>
  </assembly-descriptor>

배치 디스크립터에서 ExcludeClassInterceptors 어노테이션 또는 exclude-class-interceptors 요소를 사용하여 메소드에서 클래스 레벨 인터셉터를 제외할 수 있습니다. 다음 예를 사용하여 인터셉터 ClassInterceptor를 메소드 businessMethod에서 제외합니다.

@Interceptors({ClassInterceptor1.class})
public class TestBean2 {
	@ExcludeClassInterceptors
	public void businessMethod() { /* ... */ }

	public void businessMethodWithClassInterceptor1() { /* ... */
}

다음 예를 사용하여 배치 디스크립터를 사용한 메소드에서 인터셉터를 제외합니다.

  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>TestBean2</ejb-name>
      <exclude-class-interceptors>true</exclude-class-interceptors>
           <method>
        <method-name>businessMethod</method-name>
           </method>
    </interceptor-binding>
  </assembly-descriptor>

인터셉터 메소드에는 public, protected, package private 또는 private 표시 여부가 있을 수 있습니다. 인터셉터 메소드는 최종 또는 정적이 아니어야 합니다. 비즈니스 메소드 인터셉터 및 제한시간 메소드 인터셉터는 java.lang.Object의 리턴 유형, javax.interceptor.InvocationContext의 단일 매개변수 및 java.lang.Exception의 단일 처리 절 유형이 있어야 합니다. 모든 라이프사이클 인터셉터는 void의 리턴 유형이 있어야 하며 처리 절이 없어야 합니다. EJB 클래스에서 직접 선언된 라이프사이클 인터셉터는 매개변수가 없어야 합니다. EJB 수퍼클래스나 인터셉터 클래스에서 선언된 라이프사이클 인터셉터는 javax.interceptor.InvocationContext의 단일 매개변수가 있어야 합니다. 제한시간 인터셉터 메소드 및 라이프사이클 인터셉터 메소드는 애플리케이션 예외를 처리하지 않아야 합니다.

인터셉터 메소드의 InvocationContext 매개변수를 사용하여 호출 중인 메소드에 대한 정보를 얻을 수 있습니다. getTarget 메소드는 호출 중인 Bean 인스턴스를 리턴합니다. getTimer 메소드는 제한시간 메소드 인터셉터에만 적용되며 실행 중인 타이머를 리턴합니다. getMethod 메소드는 호출 중인 비즈니스 인터페이스 메소드를 리턴합니다. getParameters 메소드는 비즈니스 메소드에 전달 중인 매개변수를 리턴하며, setParameters 메소드를 사용하면 매개변수가 수정될 수 있습니다. getContextData 메소드는 호출 중인 메소드와의 데이터 연관을 리턴합니다. 마지막으로 진행 메소드는 다음 인터셉터 또는 대상 메소드를 호출합니다.

어노테이션 또는 XML을 사용하여 인터셉터 메소드를 선언할 수 있습니다. 어노테이션을 사용하여 인터셉터 메소드를 선언하려면, 해당 AroundInvoke, AroundTimeout, PostConstruct, PreDestroy, PrePassivate 또는 PostActivate 어노테이션을 인터셉터 메소드에 배치합니다. 다음 예를 사용하여 비즈니스 메소드 인터셉터, 제한시간 메소드 인터셉터 및 PostConstruct 라이프사이클 인터셉터 메소드를 어노테이션을 사용하여 EJB 클래스에서 선언합니다.

@Interceptors({ClassInterceptor.class})
public class TestBean {
	@PostConstruct
	private void beanPostConstruct() { /* ... */ }

	@AroundInvoke
	protected Object beanAroundInvoke(InvocationContext ic) throws Exception {
		return ic.proceed();
	}

        @AroundTimeout
        protected Object beanAroundTimeout(InvocationContext ic) throws Exception {
		return ic.proceed();
        }
}

다음 예를 사용하여 인터셉터 클래스에서 동일한 인터셉터 메소드를 선언합니다.

public class ClassInterceptor {
	@PostConstruct
	private void interceptorPostConstruct(InvocationContext ic) {
		try {
			ic.proceed();
		} catch (Exception ex) { /* ... */ }
	}

	@AroundInvoke
	protected Object interceptorAroundInvoke(InvocationContext ic) throws Exception {
		return ic.proceed();
	}

	@AroundTimeout
	protected Object interceptorAroundTimeout(InvocationContext ic) throws Exception {
		return ic.proceed();
	}
}

또는, around-invoke, around-timeout, post-construct, pre-destroy, pre-passivate 및 post-activate 요소로 배치 디스크립터에서 인터셉터 메소드를 선언할 수 있습니다. 다음 예를 사용하여 비즈니스 메소드 인터셉터, 제한시간 메소드 인터셉터 및 PostConstruct 라이프사이클 인터셉터 메소드를 배치 디스크립터를 사용하여 EJB 클래스에서 선언합니다.

  <enterprise-beans>
    <session>
      <ejb-name>TestBean</ejb-name>
      <around-invoke>
        <method-name>beanAroundInvoke</method-name>
      </around-invoke>
      <around-timeout>
        <method-name>beanAroundTimeout</method-name>
      </around-timeout>
      <post-construct>
        <lifecycle-callback-method>beanPostConstruct</lifecycle-callback-method>
      </post-construct>
    </session>
  </enterprise-beans>

  <interceptors>
    <interceptor>
      <interceptor-class>ClassInterceptor</interceptor-class>
      <around-invoke>
        <method-name>interceptorAroundInvoke</method-name>
      </around-invoke>
      <around-timeout>
        <method-name>interceptorAroundTimeout</method-name>
      </around-timeout>
      <post-construct>
        <lifecycle-callback-method>interceptorPostConstruct</lifecycle-callback-method>
      </post-construct>
    </interceptor>
  </interceptors>

  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>TestBean</ejb-name>
      <interceptor-class>ClassInterceptor</interceptor-class>
    </interceptor-binding>
  </assembly-descriptor>

수퍼클래스에서 인터셉터 메소드를 선언할 수도 있습니다. 다음 예를 사용하여 어노테이션을 사용하여 Bean 수퍼클래스에서 PostActivate 인터셉터를 선언합니다.

public class TestBean extends BeanSuperClass { /* ... */ }

public class BeanSuperClass {
	@PostActivate
	private void beanSuperClassPostActivate(InterceptorContext ic) {
		try {
			ic.proceed();
		} catch (Exception ex) { /* ... */ }
	}
}

다음 예를 사용하여 인터셉터 클래스의 수퍼클래스에서 어노테이션을 사용하여 동일한 인터셉터 메소드를 선언합니다.

public class ClassInterceptor extends InterceptorSuperClass { /* ... */ }

public class InterceptorSuperClass {
	@PostActivate
	private void interceptorSuperClassPostActivate(InterceptorContext ic) {
		try {
			ic.proceed();
		} catch (Exception ex) { /* ... */ }
	}
}

배치 디스크립터를 사용하여 동일한 인터셉터 메소드를 선언할 수도 있습니다. 다음 예를 사용하여 Bean의 인터셉터 클래스의 수퍼클래스에서 인터셉터 메소드를 선언합니다.

<enterprise-beans>
  <session>
    <ejb-name>TestBean</ejb-name>
    <post-activate>
      <class>BeanSuperClass</class>
	 <lifecycle-callback-method>beanSuperClassPostActivate</lifecycle-callback-method>
    </post-activate>
  </session>
 </enterprise-beans>

 <interceptors>
   <interceptor>
    <interceptor-class>ClassInterceptor</interceptor-class>
    <post-activate>
      <class>InterceptorSuperClass</class>
	 <lifecycle-callback-method>interceptorSuperClassPostActivate</lifecycle-callback-method>
    </post-activate>
   </interceptor>
 </interceptors>

 <assembly-descriptor>
   <interceptor-binding>
     <ejb-name>TestBean</ejb-name>
     <interceptor-class>ClassInterceptor</interceptor-class>
   </interceptor-binding>
 </assembly-descriptor>

모듈의 모든 세션 및 메시지 구동 Bean에 적용하는 기본 인터셉터를 선언할 수 있습니다. 기본 인터셉터는 배치 디스크립터에서만 선언될 수 있으며 "*"의 ejb-name을 사용하여 지정됩니다. 다음 예를 사용하여 기본 인터셉터를 선언하십시오.

  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>*</ejb-name>
      <interceptor-class>DefaultInterceptor</interceptor-class>
    </interceptor-binding>
  </assembly-descriptor>

XML에서 ExcludeDefaultInterceptors 어노테이션이나 exclude-default-interceptors 요소를 사용하여 특정 클래스나 메소드에서 기본 인터셉터를 제외할 수 있습니다. 다음 예를 사용하여 기본 인터셉터를 어노테이션을 사용하여 제외합니다.

@ExcludeDefaultInterceptors
public class TestBean { /* ... */ }

public class TestBean2 {
	@ExcludeDefaultInterceptors
	public void businessMethod() { /* ... */ }
}

다음 예를 사용하여 기본 인터셉터를 배치 디스크립터를 사용하여 제외합니다.

  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>TestBean</ejb-name>
      <exclude-default-interceptors>true</exclude-default-interceptors>
    </interceptor-binding>

    <interceptor-binding>
      <ejb-name>TestBean2</ejb-name>
      <exclude-default-interceptors>true</exclude-default-interceptors>
           <method>
        <method-name>businessMethod</method-name>
           </method>
    </interceptor-binding>
  </assembly-descriptor>

인터셉터가 메소드에 대해 호출되면, 기본 인터셉터 클래스가 먼저 호출되며, 다음으로 클래스 레벨 인터셉터가 호출되고, 마지막으로 EJB 클래스에서 인터셉터 메소드가 호출됩니다. 단일 인터셉터 클래스 계층의 경우, 인터셉터 메소드는 항상 대부분의 일반 수퍼클래스에서 먼저 호출됩니다. 기본 및 클래스 레벨 인터셉터 클래스가 배치 디스크립터 또는 인터셉터 어노테이션에서 지정된 순서로 호출됩니다. 배치 디스크립터의 interceptor-order 요소에서 기본 및 클래스 레벨 인터셉터의 전체 목록을 지정하여 이 순서 지정을 대체할 수 있습니다.

  <assembly-descriptor>
    <interceptor-binding>
      <ejb-name>TestBean</ejb-name>
      <!--
        The default ordering would be:
	  1. DefaultInterceptor
	  2. ClassInterceptor1
	  3. ClassInterceptor2

	The following stanza overrides the default ordering.
      -->
      <interceptor-order>
        <interceptor-class>ClassInterceptor2</interceptor-class>
	<interceptor-class>DefaultInterceptor</interceptor-class>
	<interceptor-class>ClassInterceptor1</interceptor-class>
      </interceptor-order>
    </interceptor-binding>
  </assembly-descriptor>

주제 유형을 표시하는 아이콘 참조 주제



시간소인 아이콘 마지막 업데이트 날짜: last_date
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=rejb_3interceptors
파일 이름:rejb_3interceptors.html