`
gigi_112
  • 浏览: 115016 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

BeanNameAutoProxyCreator拦截器拦截某个实现类方法的步骤

阅读更多

 

第一步:配置实现MethodInterceptor的切面

public class OutsideInvokeLogInterceptor implements MethodInterceptor{
	private static Logger log = Logger.getLogger("outsideInvoke");
  
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
        String methodName = invocation.getMethod().toString();
        Object returnValue = invocation.proceed();
        if (returnValue == null) {
            log.warn("调用 " + methodName + "is fail,返回的result结果为空,方法入参为:(" + parseArguments(invocation.getArguments()) + ")");
            return ResultDTO.getFailureResult(BSErrorCode.INTERNAL_ERROR, BaseKeyMsgCode.RETURN_RESULTDTO_IS_NULL);
        }else{
            if(returnValue instanceof ResultDTO){
                ResultDTO<Object> resultDTO = (ResultDTO<Object>)returnValue;
                if(!resultDTO.isSuccess()){
                    log.warn("调用 " + methodName + " is fail ,方法入参为:(" + parseArguments(invocation.getArguments()) + ")"
                         + "返回值:" +resultDTO.getCode() + "," + resultDTO.getKey());
                } else{
                    log.info("调用 " + methodName + " is success ,方法入参为:(" + parseArguments(invocation.getArguments()) + ")");
                }
            }
        }
        return returnValue;

	}
}
 

 

 

<bean id="outsideInvokeLogAdvice" class="com.ali.luna.commons.service.interceptor.OutsideInvokeLogInterceptor" />

 

第二步:配置切点


<bean id="methodPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
        <property name="mappedNames">
            <list>
                <value>add*</value>
            </list>
        </property>
    </bean>

 

第三步:配置自动代理

    <bean id="outsideInvokeLogInterceptor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="pointcut" ref="methodPointcut"/>
        <property name="advice" ref="outsideInvokeLogAdvice"/>
    </bean>
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list><value>activityService</value></list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>outsideInvokeLogInterceptor</value>
            </list>
        </property>
    </bean>

 

1
2
分享到:
评论
1 楼 a250152 2012-11-11  
不错 ,真的不错 对初学者来说!

相关推荐

    Acegi_使用.doc

    如图1所示,关键类之间相互协作,而`AbstractSecurityInterceptor`是其主要实现类。 ![Acegi Key Secure Object Model](#) **图1**:“SecureObject”模型的关键组件 #### 安全管理对象 Acegi支持三种类型的安全...

    Java实训教程 Java软件开发实战 Java开发框架 log4jdbc 共5页.pptx

    3. **invoke方法**:这是拦截器的核心方法。当方法被调用时,此方法会自动触发。如果启用了JDBC日志记录(`SpyLogFactory.getSpyLogDelegator().isJdbcLoggingEnabled()`返回`true`),并且返回的结果是一个`...

    企业级信息系统开发教学PPT(3.1)

    校验器功能通过validate方法或XML配置进行数据验证,拦截器则在请求处理过程中起到增强和过滤的作用,通过在struts.xml中配置。国际化支持通过struts.properties和相应的语言文件实现,确保多语言环境下的应用体验。...

    spring 事务代理配置

    当调用代理方法时,代理会拦截调用并自动开启、提交或回滚事务,从而实现事务的管理。 #### 配置步骤 1. **定义数据源**:首先,需要在Spring配置文件中定义数据源。数据源是所有数据库交互的基础,这里使用`...

    spring-note

    - **代理模式**:Spring 使用动态代理技术实现 AOP,通过代理对象来拦截目标对象的方法调用。 ##### 四、Spring 的通知(具体的业务逻辑) 1. **Spring 的通知类型**: - **前置通知**:在目标方法调用之前执行。...

Global site tag (gtag.js) - Google Analytics