aspectj annotation- used in spring
JAVA軟件工程師--注釋方式切面編程(Spring?AOP技術)
(2010-07-26 23:35:15)
轉載
| 標簽: spring aop 面向方面編程 web開發 java 注解 annotation 開源框架 it | 分類:JAVA高級軟件工程師 |
導論:Aspect Oriented Programming(AOP)即面向切面編程。AOP主要實現的目的是針對業務處理過程中的切面進行提取,它所面對的是處理過程中的某個步驟或階段,以獲得邏輯過程中各部分之間低耦合性的隔離效果。它使得代碼更加靈活,并且提供了代碼的重用性。
????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 作者: 徐亮
可以寫普通JAVA類+配置文件的方式進行切面編程,也可以用注釋(annotation)方式進行切面編程。本例中采用后者。
流程:
1、寫特殊的JAVA類,它通過添加注釋(annotation)方式,指定切面、切入點和通知----->
2、在配置文件中加入一行,以便打開aspectJ注解,使其自動生成代理------>
3、寫目標類的目標方法----->
4、測試類中調用目標類的目標方法。
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("環繞通知上。。。");
??? ??? Object obj=pjp.proceed();
??? ??? System.out.println("環繞通知下。。。");
??? ??? 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、寫目標類的目標方法
public class MyService implements IMyService {
??? public void regist(){
??? ??? try {
??? ??? ??? Thread.sleep(2000);
??? ??? } catch (InterruptedException e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? System.out.println("----執行注冊操作----");
??? }
}
4、測試類中調用目標類的目標方法。
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的方法就會執行下面的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 "";
}
總結
以上是生活随笔為你收集整理的aspectj annotation- used in spring的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ant的if-else
- 下一篇: spring autoproxy by