在EJB3中编写拦截器以及如何配置默认拦截器

1.编写拦截器,主要是把进入的类和方法写入日志中

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HpTracingInterceptor {

	private static final Logger LOG = LoggerFactory.getLogger(HpTracingInterceptor.class);

	/**
	 * this method is wrapped around a method call and measures the time the
	 * call needs to run.
	 * 
	 * @param invocationContext
	 *            invocation context
	 * @return callback of context.proceed()
	 * @throws Exception
	 *             if the method call raises an exception
	 */
	@AroundInvoke
	private Object measurePerformance(InvocationContext _invocationContext) throws Exception {

		String className = _invocationContext.getTarget().getClass().getSimpleName();
		String methodName = _invocationContext.getMethod().getName();
		Object _obj = null;
		try {
			LOG.debug("Invoking method: {} from class: {}", methodName, className);
			_obj = _invocationContext.proceed();
		} catch (Exception e) {
			LOG.error("!!!During invocation of: {} exception occured: {}", methodName, e);
			throw e;
		}
		LOG.debug("Finished method :{}", methodName);
		return _obj;
	}
}

2.在jboss-ejb3.xml中配置那些类拦截器哪些排除该拦截器.

<?xml version="1.1" encoding="UTF-8"?>
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
	xmlns:p="urn:ejb-pool:1.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
	version="3.1" impl-version="2.0">
	<enterprise-beans>
	</enterprise-beans>

	<interceptors>
             <!-- 定义该拦截器 -->
             <interceptor>
		<interceptor-class>com.ufis_as.ufisapp.server.interception.HpTracingInterceptor</interceptor-class>
	     </interceptor>
	</interceptors>


	<assembly-descriptor>
            <!-- 配置默认的拦截器-->
	    <interceptor-binding>
		<ejb-name>*</ejb-name>
		<interceptor-class>com.ufis_as.ufisapp.server.interception.HpTracingInterceptor</interceptor-class>
	    </interceptor-binding>
	    
            <!-- 排除默认的拦截器 -->
            <interceptor-binding>
                <!-- ejb名字貌似只支持'*'这种通配符,其他的只能用具体的ejb名 -->
<ejb-name>DlAbstractBean</ejb-name><exclude-default-interceptors>true</exclude-default-interceptors> </interceptor-binding></assembly-descriptor></jboss:ejb-jar>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值