AOP AspectJ註解

來源:互聯網
上載者:User

標籤: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);}@Componentclass 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@Aspectclass 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

AOP AspectJ註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.