Aspectj的AOP操作

来源:互联网 发布:电脑屏幕视频录像软件 编辑:程序博客网 时间:2024/05/21 06:49

导包和约束

这里写图片描述

这里写图片描述

基于配置文件的AOP操作

//Book.javapackage cn.cstor;public class Book {    public void show(){        System.out.println("come from Book.show()...");        //int i = 1/0;    }}
//EnhancedBook.javapackage cn.cstor;import org.aspectj.lang.ProceedingJoinPoint;public class EnhancedBook {    public void before() {        System.out.println("come from EnhancedBook.before()..");    }    public void after() {        System.out.println("come from EnhancedBook.after()..");    }    public void afterReturnling() {        System.out.println("come from EnhancedBook.afterReturnling()..");    }    public void throwing() {        System.out.println("come from EnhancedBook.throwing()..");    }    public void around(ProceedingJoinPoint joinPoint) throws Throwable {        System.out.println("come from EnhancedBook.around() start...");        joinPoint.proceed();        System.out.println("come from EnhancedBook.around() end...");    }}
<!-- applicationContext.xml --><?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">    <!-- 创建Bean对象 -->    <bean id="book" class="cn.cstor.Book"/>    <bean id="enhancedBook" class="cn.cstor.EnhancedBook"/>    <!-- AOP配置-->    <aop:config>        <!-- 配置切入点 -->        <aop:pointcut expression="execution(* cn.cstor.Book.*(..))" id="pointcut1"/>        <!-- 配置切面 -->        <aop:aspect ref="enhancedBook">            <aop:before method="before" pointcut-ref="pointcut1"/>            <aop:after method="after" pointcut-ref="pointcut1"/>            <aop:after-returning method="afterReturnling" pointcut-ref="pointcut1"/>            <aop:after-throwing method="throwing" pointcut-ref="pointcut1"/>            <aop:around method="around" pointcut-ref="pointcut1"/>        </aop:aspect>    </aop:config></beans>
//BookTest.javapackage cn.cstor;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class BookTest {    @Test    public void f(){        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");        ((Book) ac.getBean("book")).show();    }}

基于注解的AOP操作

<!-- applicationContext.xml --><?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">    <bean id="book" class="cn.cstor.Book"/>    <bean id="enhancedBook" class="cn.cstor.EnhancedBook"/>    <!-- 开启aop操作 -->    <aop:aspectj-autoproxy/></beans>
//EnhancedBook.javapackage cn.cstor;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class EnhancedBook {    @Before("execution(* cn.cstor.Book.*(..))")    public void before() {        System.out.println("come from EnhancedBook.before()..");    }    @After("execution(* cn.cstor.Book.*(..))")    public void after() {        System.out.println("come from EnhancedBook.after()..");    }    @AfterReturning("execution(* cn.cstor.Book.*(..))")    public void afterReturnling() {        System.out.println("come from EnhancedBook.afterReturnling()..");    }    @AfterThrowing("execution(* cn.cstor.Book.*(..))")    public void throwing() {        System.out.println("come from EnhancedBook.throwing()..");    }    @Around("execution(* cn.cstor.Book.*(..))")    public void around(ProceedingJoinPoint joinPoint) throws Throwable {        System.out.println("come from EnhancedBook.around() start...");        joinPoint.proceed();        System.out.println("come from EnhancedBook.around() end...");    }}