JavaEE笔记(十二)

JavaEE笔记(十二)

代理的三种配置

beans配置文件

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.2.xsd">


    <bean id="IStudentService" class="com.my.servic.impl.StudentServiceImpl"></bean>
    <bean id="IEmpService" class="com.my.servic.impl.EmpServiceImpl"></bean>
    <bean id="logAdivce" class="com.my.advice.LogAdivce"></bean>
    <bean id="customerAdvice" class="com.my.advice.CustomerAdvice"></bean>
    <bean id="annotationAdvice" class="com.my.advice.AnnotationAdvice"></bean>
    <aop:config>
        <aop:pointcut expression="execution(* com.my.servic.impl.*.*(..))"
            id="pointcut" />
        <aop:advisor advice-ref="logAdivce" pointcut-ref="pointcut" />
    </aop:config>
    <!-- 自定义配置通知 -->

    <aop:config>
        <aop:aspect ref="customerAdvice">
            <aop:pointcut expression="execution(* 
        com.my.servic.impl.*.add(..))"
                id="pointcut1" />
            <aop:pointcut expression="execution(* 
        com.my.servic.impl.*.delete(..))"
                id="pointcut2" />
            <aop:around method="roundMethod" pointcut-ref="pointcut1" />
            <aop:before method="methodBefore" pointcut-ref="pointcut1" />
            <aop:after method="methodAfter" pointcut-ref="pointcut2" />
        </aop:aspect>
    </aop:config>

    <!-- 扫描注解 组件 -->
    <context:component-scan base-package="*"></context:component-scan>

    <!-- 注解方式 -->
    <aop:aspectj-autoproxy />

    <!-- 使用ProxyFactoryBean -->

    <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <list>
                <value>com.my.servic.IStudentService</value>
                <value>com.my.servic.IEmpService</value>
            </list>
        </property>

        <property name="target">
            <ref local="IStudentService" />
        </property>
        <property name="interceptorNames">
            <list>
                <value>logAdivce</value>
            </list>
        </property>
    </bean>
</beans>

注解写法

package com.my.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class AnnotationAdvice {
    
    @Before("execution(* com.my.servic.impl.*.*(..))")
    public void before(JoinPoint jp){
        System.out.println("方法调用前。。");
        System.out.println(jp.getTarget()+"  +++>>> "+jp.getSignature()+"   "+jp.getArgs());
    }
    
    @Around("execution(* com.my.servic.impl.*.*(..))")
    public void round(ProceedingJoinPoint pjp){
        System.out.println("环绕前。。");
        try {
            pjp.proceed();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("环绕后前。。");
    }

}

自定义写法

package com.my.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;


public class CustomerAdvice {
    
    public void methodBefore(JoinPoint jp){
        System.out.println("方法调用前。。");
        System.out.println(jp.getTarget()+"  +++>>> "+jp.getSignature()+"   "+jp.getArgs());
        
    }
    
    public void methodAfter(JoinPoint jp){
        System.out.println("方法调用后。。");
    }
    
    public void roundMethod(ProceedingJoinPoint pjp){
        System.out.println("环绕前。。");
        
        try {
            pjp.proceed();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("环绕后。。");
    }

}

常规实现接口写法

package com.my.advice;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import com.my.bean.Emp;
import com.my.bean.Student;

public class LogAdivce implements MethodBeforeAdvice, AfterReturningAdvice,
        MethodInterceptor {

    @Override
    public void before(Method method, Object[] arg1, Object target)
            throws Throwable {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        System.out.println("目标对象" + target.getClass().getName() + "的"
                + method.getName() + " 在" + sf.format(new Date()) + " 被调用了");

        for (Object obj : arg1) {

            if (obj.getClass().getName().equals("com.my.bean.Student")) {
                Student stu = (Student) obj;
                System.out.println("参数为:" + stu.getName());
            } else if (obj.getClass().getName().equals("com.my.bean.Emp")) {
                Emp e = (Emp) obj;
                System.out.println("参数为:" + e.getName());
            }
        }
    }

    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        // TODO Auto-generated method stub
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("环绕前。。");
        Object obj = methodInvocation.proceed();
        System.out.println("环绕后。。");
        return obj;
    }

}

 

posted @ 2016-12-09 00:02 dawn-tangzedong 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值