<六>AOP面向切面——注解方式声明切面(附源码)

本文介绍了如何在Spring框架中使用注解声明面向切面编程的切面,并通过一个具体的例子展示了在beans.xml中配置切面bean以及进行测试的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、beans.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:aop="http://www.springframework.org/schema/aop"								//新加入

       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
		   http://www.springframework.org/schema/context/spring-context-2.5.xsd	
           http://www.springframework.org/schema/aop										//新加入
		   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">					//新加入
        <aop:aspectj-autoproxy/>								//新加入,开启spring的拦截配置
        <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>
        <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
</beans>

2、service层以及实现层如下:

PersonService.java

package cn.itcast.service;

public interface PersonService {
	public void save(String name);
	public void update(String name, Integer id);
	public String getPersonName(Integer id);
}
PersonServiceBean.java

package cn.itcast.service.impl;

import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {

	public String getPersonName(Integer id) {
		System.out.println("我是getPersonName()方法");
		return "ning";
	}

	public void save(String name) {
		//throw new RuntimeException("我爱例外");
		System.out.println("我是save()方法");
	}

	public void update(String name, Integer id) {
		System.out.println("我是update()方法");
	}

}

3、切面

声明一个切面,还需要在bean.xml文件中声明如下语句 <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>,表示将该切面交给spring管理

package cn.itcast.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;
/**
 * 切面
 *
 */
//声明一个切面,还需要在bean.xml文件中声明如下语句
// <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>
//表示将该切面交给spring管理
@Aspect
public class MyInterceptor {
	//声明一个切入点,execution代表执行,执行业务方法时候进行拦截
	//* cn.itcast.service..*.*(..)
	//第一个*表示返回值类型,没有具说明就是所有类型; 
	//cn.itcast.service表示包名,对该包下的类进行拦截;接下来两个点..代表子包
	//第二个*表示类,对该类进行拦截,没有具体指定则用*号表示,代表所有类
	//第三个*代表方法,对该方法进行拦截,没有具体指定则用*号表示,代表所有方法
	//最后两点..表示方法的参数
	//以下一句表示,任意返回值,对cn.itcast.service.impl包下的PersonServiceBean类的*所有方法进行拦截
	@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")
	private void anyMethod() {}//声明一个切入点
	
	//前置通知,拦截方法执行前,先执行该方法。若要自己加参数,则使用&&符号,
	//这意味着在cn.itcast.service.impl.PersonServiceBean类中的方法,只会执行符合参数定义的方法
	//通过在SpringAOPTest中的save("chenning")方法会让前置通知得到该参数
	@Before("anyMethod() && args(name)")
	public void doAccessCheck(String name) {
		System.out.println("前置通知:"+name);
	}
	//后置通知
	@AfterReturning(pointcut="anyMethod()",returning="chen")
	public void doAfterReturning(String chen)
	{
		System.out.println("后置通知"+chen);
	}
	//最终通知
	@After("anyMethod()")
	public void doAfter(){
		System.out.println("最终通知");
	}
	//例外通知
	@AfterThrowing(pointcut="anyMethod()",throwing="e")
	public void doAfterThrowing(Exception e){
		System.out.println("例外通知"+e);
	}
//	//环绕通知
	@Around("anyMethod()")
	public Object doBasicProfilling(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("进入方法");
		Object result=pjp.proceed();
		System.out.println("退出方法");
		return result;
	}
}

4、测试SpringAOPTest.java

package junit.test;


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

import cn.itcast.service.PersonService;

public class SpringAOPTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)cxt.getBean("personService");
		personService.save("chenning");
		personService.getPersonName(2);
	}
}
5、控制台输出:

前置通知:chenning
进入方法
我是save()方法
后置通知null
最终通知
退出方法
进入方法
我是getPersonName()方法
后置通知ning
最终通知
退出方法

完成。

源码下载:
点击打开链接



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值