标签:bre bean ack .class turn ret bsp com 3.0
Code:
package com.qhong; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person=(Person)context.getBean("babyPerson"); person.eatBreakfast(); System.out.println("==================================================="); person.eatLunch(); System.out.println("==================================================="); person.eatSupper(); System.out.println("==================================================="); person.drink("可乐"); System.out.println("==================================================="); } } interface Person { public void eatBreakfast(); public void eatLunch(); public void eatSupper(); public String drink(String name); } @Component class BabyPerson implements Person{ @Override public void eatBreakfast() { System.out.println("小Baby正在吃早餐"); } @Override public void eatLunch() { System.out.println("小Baby正在吃午餐"); } @Override public void eatSupper() { System.out.println("小Baby正在吃晚餐"); } @Override public String drink(String name) { return "小Baby在喝:"+name; } } @Component @Aspect class AdivceMethod { @Before("execution(* com.qhong.BabyPerson.*(..))") // 匹配BabyPerson类所有的方法,注意*和com之间有个空格 public void beforeEat() { System.out .println("-------------------这里是前置增强,吃饭之前先洗小手!--------------------"); } @After("execution(* eatLunch(..))") // 匹配该工程下所有的eatLunch方法 public void afterEat() { System.out .println("-------------------这里是后置增强,午饭吃完要睡午觉!--------------------"); } @Around("execution(* com.qhong.BabyPerson.eatSupper())") // 匹配该工程下BabyPerson的eatLunch方法 public Object aroundEat(ProceedingJoinPoint pjp) throws Throwable { System.out .println("-------------------这里是环绕增强,吃晚饭前先玩一玩!-------------------"); Object retVal = pjp.proceed(); System.out .println("-------------------这里是环绕增强,晚饭吃完后要得睡觉了!-------------------"); return retVal; } @AfterReturning(returning="rvt",pointcut="execution(* com.qhong.BabyPerson.drink(..))") public void log(Object rvt) { System.out .println("-------------------这里是AfterReturning增强-------------------"); System.out.println("获取小Baby正在喝的饮料"+rvt); System.out.println("记录每天喝的饮料容量"); } }
beans.xml
<?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: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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 指定自动搜索bean组件、自动搜索切面类 --> <context:component-scan base-package="com.qhong"/> <!-- 启动@AspectJ支持 --> <!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 --> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
http://tonylit.me/2016/06/29/spring%20aop-AspectJ%E6%B3%A8%E8%A7%A3%E6%96%B9%E5%BC%8F/
http://www.yiibai.com/spring/spring-aop-aspectj-annotation-example.html
http://blog.csdn.net/xiaoxian8023/article/details/17285809
http://www.kancloud.cn/evankaka/springlearning/119670
标签:bre bean ack .class turn ret bsp com 3.0
原文地址:http://www.cnblogs.com/hongdada/p/6512589.html