javaEE之------ApectJ的切面技術===標籤

來源:互聯網
上載者:User

標籤:位置   ram   jsb   切面   .class   代理類   method   ns-3   pack   

如今比較流行了aop技術之中的一個========標籤

實現步驟:一,匯入aop標籤

方法,開啟aop包。裡面就有。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >這個裡面就有


然後依據選擇spring的版本號碼。

在設定檔裡配置

例如以下:

<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:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

這樣就匯入了aop標籤


二,配置切點和通知

<aop:config><aop:aspect ref="myadvior"><!-- 須要匯入作為切面的類--><aop:pointcut expression="execution(* cn..Person.*(..))" id="cut"/><aop:before method="test1" pointcut-ref="cut"/> <!-- 這是通知,攔截切點位置。也就是攔截時  須要做的事情 --><aop:aftermethod="test1" pointcut-ref="cut"/> <!-- 攔截核心之後啟動並執行動作,所有寫在test1方法裡面了 --><!-- <aop:before method="test1" pointcut="execution(* cn..Person.*(..))"/> 這樣也是能夠的,就不用切點了,直接寫在這裡面 --></aop:aspect></aop:config>
裡面的配置通知類型非常多

三。被代理類以及自己主動註解類

<span style="font-size:18px;"><!-- 須要的三元素 (被代理類, 自己主動代理類,切面)採用標籤。切面和之前的有點不同。僅僅是一個簡單的pojo匯入 --><bean id="person" class="cn.aop.aspectj3.Person"></bean></span>
<span style="font-size:18px;"> <!-- 註解自己主動標籤,自己主動去尋找帶有註解的類和方法 --><span style="white-space:pre"></span> <aop:aspectj-autoproxy></aop:aspectj-autoproxy></span>

四。匯入我們作為切面的類
<bean class="cn.aop.aspectj3.MyAdvisor" id="myadvior"/><!--當做切面的pojo 注入 -->

設定檔已經完畢。


五,被匯入的作為切面的類

public class MyAdvisor {public void test1(){System.out.println("這是test...");}}
非常普通的一類。方法名在配置切面裡面,通知的時間也完畢了。。這裡就能夠實現想要完畢的動作了。

標籤相當於之前的,有非常大的最佳化,如在核心模組全然不知道是都做了攔截,進一步實現瞭解耦。

=========================這裡已經介紹完====================



源碼以及測試

1,設定檔

<?

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:tx="http://www.springframework.org/schema/tx"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><!-- 須要的三元素 (被代理類。 自己主動代理類,切面)採用標籤,切面和之前的有點不同,僅僅是一個簡單的pojo匯入 --><bean id="person" class="cn.aop.aspectj3.Person"></bean><!-- 自己主動代理註解<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> --> <!-- 註解自己主動標籤,自己主動去尋找帶有註解的類和方法 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <aop:config><aop:aspect ref="myadvior"><aop:pointcut expression="execution(* cn..Person.*(..))" id="cut"/><aop:before method="test1" pointcut-ref="cut"/> <!-- 這是通知,攔截切點位置。也就是攔截時 須要做的事情 --><aop:aftermethod="test1" pointcut-ref="cut"/> <!-- 攔截核心之後啟動並執行動作。所有寫在test1方法裡面了 --><!-- <aop:before method="test1" pointcut="execution(* cn..Person.*(..))"/> 這樣也是能夠的。就不用切點了,直接寫在這裡面 --></aop:aspect></aop:config><bean class="cn.aop.aspectj3.MyAdvisor" id="myadvior"/><!--當做切面的pojo 注入 --></beans>

     2,  作為切面的類

package cn.aop.aspectj3;public class MyAdvisor {public void test1(){System.out.println("這是test...");}}
3,被代理的對象

package cn.aop.aspectj3;public class Person {public void say(){System.out.println("...這是say..");}public void run(){System.out.println("這是person中的 run方法");}}
4,測試

@Testpublic void Test2(){ApplicationContext context =new ClassPathXmlApplicationContext("cn/aop/aspectj3/aspectj3.xml");Person p =context.getBean(Person.class);p.run();p.say();}




javaEE之------ApectJ的切面技術===標籤

聯繫我們

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