Apring中AOP使用

在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!");
		}

	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值