JavaEE筆記(十二)

來源:互聯網
上載者:User

標籤:[]   add   todo   context   cep   base   oda   exp   oca   

代理的三種配置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@Aspectpublic 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;    }}

 

JavaEE筆記(十二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.