spring AOP实例

一个简单的AOP实例

全例以注解形式完成spring配置


1.创建了一个业务接口

BuyBook.java

package aop;

public interface BuyBook {
	public String buyBook(String customer , String book);
}


2.实现接口

BuyBookImpl.java

package aop;

import org.springframework.stereotype.Service;
@Service
public class BuyBookImpl implements BuyBook {
	public String buyBook(String customer, String book) {
		System.out.println("customer:"+customer+","+"book:"+book);
		return "Hello World!";
	}
}



3.创建切面类

MoocAspect.java

package aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
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 MoocAspect {
	
	@Pointcut("execution(* aop.BuyBookImpl.*(..))")
	public void pointcut(){}
	
	//传入参数customer和book,参数需与aspect类方法参数相同
	@Before("pointcut() && args(customer,book)")
	public void before(String customer,String book){
		System.out.println("Before"+book);
	}
	
	@After("pointcut()")
	public void after(){
		System.out.println("After");
	}
	
	@Around("pointcut()")
	public Object aroundAspect(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("around 1");
		Object obj = pjp.proceed();
		System.out.println("around 2");
		System.out.println("around"+obj);
		return obj;
	}
	
	@AfterReturning(pointcut="pointcut()",returning="returnValue")
	public void afterReturning(Object returnValue){
		System.out.println("AfterReturning:"+returnValue);
	}
	
}



4.创建测试类

TestBuyBook.java

package aop;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBuyBook {
	@Test
	public void testBuyBook(){
		String location = "applicationContext.xml";
		ApplicationContext ctx = new ClassPathXmlApplicationContext(location);
		BuyBook b = (BuyBook) ctx.getBean("buyBookImpl");
		b.buyBook("小东", "《楚留香》");
		
	}
}

5.spring配置xml

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
	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.1.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd">
	<context:component-scan base-package="aop"></context:component-scan> 
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
		
</beans>


执行结果:

Before《楚留香》
around 1
customer:小东,book:《楚留香》
After
AfterReturning:Hello World!
around 2
aroundHello World!




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值