日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

aspectj annotation- used in spring

發(fā)布時間:2024/4/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aspectj annotation- used in spring 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JAVA軟件工程師--注釋方式切面編程(Spring?AOP技術(shù))

(2010-07-26 23:35:15)

轉(zhuǎn)載

標(biāo)簽:

spring

aop

面向方面編程

web開發(fā)

java

注解

annotation

開源框架

it

分類:JAVA高級軟件工程師

導(dǎo)論:Aspect Oriented Programming(AOP)即面向切面編程。AOP主要實現(xiàn)的目的是針對業(yè)務(wù)處理過程中的切面進(jìn)行提取,它所面對的是處理過程中的某個步驟或階段,以獲得邏輯過程中各部分之間低耦合性的隔離效果。它使得代碼更加靈活,并且提供了代碼的重用性。
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 作者: 徐亮
可以寫普通JAVA+配置文件的方式進(jìn)行切面編程,也可以用注釋(annotation)方式進(jìn)行切面編程。本例中采用后者。
流程:
1、寫特殊的JAVA類,它通過添加注釋(annotation)方式,指定切面、切入點和通知----->
2、在配置文件中加入一行,以便打開aspectJ注解,使其自動生成代理------>
3、寫目標(biāo)類的目標(biāo)方法----->
4、測試類中調(diào)用目標(biāo)類的目標(biāo)方法。

1、寫特殊的JAVA類,它通過添加注釋(annotation)方式

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;
@Aspect
//指定這是一個其切面
public class myAnnotationAspect {

//指定切入點
??? @Pointcut("execution(* myTarena.*.*(..))")
??? public void myPointCut(){}

? //指定通知以及通知的范圍
??? @Before("myPointCut()")
??? public void beforeMethod(){
??? ??? System.out.println("前置通知。。。");
??? }
???
??? @Around("myPointCut()")
??? public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
??? ??? System.out.println("環(huán)繞通知上。。。");
??? ??? Object obj=pjp.proceed();
??? ??? System.out.println("環(huán)繞通知下。。。");
??? ??? return obj;
??? }
???
??? @After("myPointCut()")
??? public void afterMethod(){
??? ??? System.out.println("后置通知。。。");
?? ???
??? }???
}

2、在配置文件中加入一行,以便打開aspectJ注解,使其自動生成代理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
??? xmlns:aop="http://www.springframework.org/schema/aop"
??? xmlns:context="http://www.springframework.org/schema/context"
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
??? 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 id="myService" class="myTarena.MyService"></bean>
?????????? <bean id="myAnnotationAspect" class="myTarena.myAnnotationAspect"></bean>
????????? <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

3、寫目標(biāo)類的目標(biāo)方法
public class MyService implements IMyService {
??? public void regist(){
??? ??? try {
??? ??? ??? Thread.sleep(2000);
??? ??? } catch (InterruptedException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? System.out.println("----執(zhí)行注冊操作----");
??? }
}

4、測試類中調(diào)用目標(biāo)類的目標(biāo)方法。
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectTest {
??? public static void main(String[] args) {
??? ??? ApplicationContext ctx=new ClassPathXmlApplicationContext("aop-aspect.xml");
??? ??? IMyService myService=(IMyService) ctx.getBean("myService");
??? ??? myService.regist();

??? }
}

@Aspect

public class AuditPointcut {

????? @Pointcut(value="@annotation(audit)", argNames="audit")

??? public void auditMethod(Audit audit) {}

}

表示加了注釋audit的方法就會執(zhí)行下面的doAudit方法

public class AuditAdvice implements springframework.core.Ordered{

@Around(value = "AuditPointcut.auditMethod(audit)", argNames = "pjp, audit")

????? public Object doAudit(ProceedingJoinPoint pjp, Audit audit) throws Throwable {

?????

}

}

?

定義注釋

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface Audit {

??? String operation() default "";

??? String component() default "";

??? String comments() default "";

}

總結(jié)

以上是生活随笔為你收集整理的aspectj annotation- used in spring的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。