您的位置:首页 > 运维架构

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

2014-11-30 18:14 537 查看
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
最终通知
退出方法


完成。

源码下载:

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: