Apring中AOP使用
2015-08-21 17:05
549 查看
在XML中配置切面
参见下图一、声明前置和后置通知
其中展示了两种声明方法可以直接声明<aop:before pointcut="execution(* SpringStudy.Model.Counter.perform(..))"
method="turnOffCellPhones" />
也可以先声明切点<aop:pointcut id="perform"
expression="execution(* SpringStudy.Model.Counter.perform(..))" />
然后用pointcut-ref指向该切点
<aop:config> <aop:aspect id="aspect" ref="audience"> <aop:pointcut id="perform" expression="execution(* SpringStudy.Model.Counter.perform(..))" /> <aop:before pointcut-ref="perform" method="takeSeats" /> <aop:before pointcut="execution(* SpringStudy.Model.Counter.perform(..))" method="takeSeats" /> <aop:before pointcut="execution(* SpringStudy.Model.Counter.perform(..))" method="turnOffCellPhones" /> <aop:after-returning pointcut="execution(* SpringStudy.Model.Counter.perform(..))" method="applaud" /> </aop:aspect> </aop:config>
Counter
package SpringStudy.Model; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component public class Counter { public void perform() { System.out.println("performing"); } }Audience
package SpringStudy.Model.Aop; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; @Component public class Audience { public void takeSeats() { System.out.println("The audiene is taking their seats."); } public void turnOffCellPhones() { System.out.println("The audiene is turning off their cellphones."); } public void applaud() { System.out.println("CLAP"); } public void demandRefund() { System.out.println("Boo! we want our money back!"); } }测试类
public class SpringTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容 Counter c = ctx.getBean("counter", Counter.class);// 创建bean的引用对象 c.perform(); } }
二、声明环绕通知
<aop:config> <aop:aspect id="aspect" ref="audienceNew"> <aop:pointcut id="perform" expression="execution(* SpringStudy.Model.Counter.perform(..))" /> <aop:around pointcut-ref="perform" method="watchPerformance" /> </aop:aspect> </aop:config>
AudienceNew类,其他类同上
package SpringStudy.Model.Aop; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; @Component public class AudienceNew { public void watchPerformance(ProceedingJoinPoint joinpoint) { try { System.out.println("The audiene is taking their seats."); System.out.println("The audiene is turning off their cellphones."); long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis(); System.out.println("CLAP"); } catch (Throwable t) { System.out.println("Boo! we want our money back!"); } } }
注解切面
@Aspect标注一个类,表明这个类是一个切面@Pointcut注解用于定义一个可以在@AspectJ切面内可以重用的切点,注解值是一个AspectJ表达式
@Before前置通知
@AfterReturing后置通知
@AfterThrowing抛异常时调用
@Around环绕通知
package SpringStudy.Model.Aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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.Component;
@Component
@Aspect
public class NewAudience {
@Pointcut("execution(* SpringStudy.Model.Counter.perform(..))")
public void performance() {
}
@Before("performance()")
public void takeSeats() {
System.out.println("The new audiene is taking their seats.");
}
@Before("performance()")
public void turnOffCellPhones() {
System.out.println("The new audiene is turning off their cellphones.");
}
@AfterReturning("performance()")
public void applaud() {
System.out.println("new CLAP");
}
@AfterThrowing("performance()")
public void demandRefund() {
System.out.println("new Boo! we want our money back!");
}
@Around("performance()")
public void watchPerformance(ProceedingJoinPoint joinpoint) {
try {
System.out.println("around The audiene is taking their seats.");
System.out.println("around The audiene is turning off their cellphones.");
long start = System.currentTimeMillis();
joinpoint.proceed();
long end = System.currentTimeMillis();
System.out.println("around CLAP");
} catch (Throwable t) {
System.out.println("around Boo! we want our money back!");
}
}
}
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- yui3的AOP(面向切面编程)和OOP(面向对象编程)
- JavaScript AOP编程实例
- 使用AOP改善javascript代码
- 初识SmartJS - AOP三剑客
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- java使用动态代理来实现AOP(日志记录)的实例代码
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- JavaScript之AOP编程实例
- Javascript aop(面向切面编程)之around(环绕)分析
- php笔记之:AOP的应用
- 基于Spring框架的Shiro配置方法