`
1028826685
  • 浏览: 920657 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类

基于注解方式声明切面(AOP)

阅读更多
基础知识:


首先启动对@AspectJ注解的支持(蓝色部分):
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/>
<bean id="log" class="cn.itcast.service.LogPrint"/>
</beans>

@Aspect
public class LogPrint {
@Pointcut("execution(* cn.itcast.service..*.*(..))")
private void anyMethod() {}//声明一个切入点
@Before("anyMethod() && args(userName)")//定义前置通知
public void doAccessCheck(String userName) {
}
@AfterReturning(pointcut="anyMethod()",returning="revalue")//定义后置通知
public void doReturnCheck(String revalue) {
}
@AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义例外通知
    public void doExceptionAction(Exception ex) {
}
@After("anyMethod()")//定义最终通知
public void doReleaseAction() {
}
@Around("anyMethod()")//环绕通知
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
return pjp.proceed();
}
}



照样实现步骤如下:

第一步:导入需要的*.jar



dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar



第二步:根据上面的基础知识导入命名空间:
       xmlns:aop="http://www.springframework.org/schema/aop"
      http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
//打开spring aop
<aop:aspectj-autoproxy/>


第三步:编写我们交个spring管理的拦截器类

@Aspect//声明切面
public class MyItercepter {

//下面是AOP表达式
@Pointcut("execution (* com.liyong.serviceBean.Imp.PersonServiceBean.*(..))")

private void anyMethod() {}//声明一个切入点这是切入点名称

//下面的前置通知表达式意思是满足anyMethod()切入点并且调用函数参数类型是String
@Before("anyMethod() && args(name)")
public void doAccessCheck(String name) {
//在执行拦截方法前调用可得到输入参数
System.out.println("name : "+name);
System.out.println("exctution 前置通知");
}
//得到返回的结果 returning 是调用方法返回的结果作为doReturnCheck()函数的输入参数
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doReturnCheck(String result) {
//在执行拦截方法后调用可得到返回参数
System.out.println("exctution 后置通知");
System.out.println("result : "+result);
}

@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doExceptionAction(Exception e) {
System.out.println("e : "+e);
System.out.println("exctution 异常通知");
}

@After("anyMethod()")
public void doReleaseAction() {
System.out.println("exctution 最终通知");
}

@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("exctution 开始环绕测试");
//必须调用下面的方法
Object object=pjp.proceed();
System.out.println("exctution 结束环绕测试");
return object;
}


}

第四步 :编写业务类和接口

IPersonServiceBean。java PersonServiceBean。java

public interface IPersonServiceBean {

public abstract void save(String name);

public abstract String update(String name);

}

public class PersonServiceBean implements IPersonServiceBean {

public void save(String name)
{
// throw new IllegalArgumentException("抛出异常");
System.out.println("save is invoke");
}

public String update(String name)
{

System.out.println("update is invoke");
return "Sueccess";
}
}

第五步:在这里我们使用xml文件的形式来办bean交个spring管理(还有可以通过类路径扫描让spring管理bean见上面的博客)


<?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
     <bean id="personService" class="com.liyong.serviceBean.Imp.PersonServiceBean"/>
<bean id="myItercepter" class="com.liyong.Itecepter.MyItercepter"/>

</beans>

第六步:编写单元测试

@Test
public void TestAOP()
{
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
IPersonServiceBean personServiceBean =(IPersonServiceBean)context.getBean("personService");
//personServiceBean.save("liyong");
personServiceBean.update("xxx");
}

总结:

这里面最重要和核心的就是怎样编写我们的拦截器类代码然后交个spring管理就OK啦
知识讲解:

AOP表达式:

对某个包及类作拦截:

   @Pointcut("execution (* com.liyong.serviceBean.Imp.PersonServiceBean.*(..))")
private void anyMethod() {}//声明一个切入点这是切入点名称

在上面的切入点execution()方法中第一个*表示这个切入点函数返回的类型为任意类型
com.liyong.serviceBean.Imp表示包.PersonServiceBean表示这个包下的类
其中我们也可以这么写com.liyong.serviceBean.Imp.*表示这个包及其子包
.*(..)表示这个类中的所有方法..表示参数任意类型和个数




@Before("anyMethod() && args(name)")
public void doAccessCheck(String name) {
//在执行拦截方法前调用可得到输入参数
System.out.println("name : "+name);
System.out.println("exctution 前置通知");
}
@Before表示前置通知 anyMethod()表示这个切入点的名称类似函数

&& args(name)表示不仅满足anyMethod()这个切入点还满足这个函数参数类型是String





//得到返回的结果 returning 是调用方法返回的结果作为doReturnCheck()函数的输入参数
@AfterReturning(pointcut="anyMethod()",returning="result")
public void doReturnCheck(String result) {
//在执行拦截方法后调用可得到返回参数
System.out.println("exctution 后置通知");
System.out.println("result : "+result);
}





例外通知:
第一个指明切入点是:anyMethod()
第二个参数:throwing="e" 表示这个函数抛出一个异常 作为doExceptionAction(..)
函数的输入值
@AfterThrowing(pointcut="anyMethod()",throwing="e")
public void doExceptionAction(Exception e) {
System.out.println("e : "+e);
System.out.println("exctution 异常通知");
}





最终通知:

anyMethod():切入点名称
@After("anyMethod()")
public void doReleaseAction() {
System.out.println("exctution 最终通知");
}



环绕通知:

@Around("anyMethod()")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("exctution 开始环绕测试");
//必须调用下面的方法
Object object=pjp.proceed();
System.out.println("exctution 结束环绕测试");
return object;
}



总结各个通知执行顺序:

第一种:没有环绕通知

当某个方法被拦截到了先执行->前置通知->执行这个拦截函数->(如果出现例外就执行
例外通知)->最终通知

第二种:
<1、有环绕通知没有异常情况下
当某个方法被拦截到了先执行->前置通知->执行到环绕通知里的方法
Object object=pjp.proceed();前面的代码
调用这个函数pjp.proceed();
然后去执行这个拦截的方法
然后执行后置通知
最终通知
然后执行Object object=pjp.proceed();后面的代码

<2、有环绕通知有异常情况下

当某个方法被拦截到了先执行->前置通知->执行到环绕通知里的方法
Object object=pjp.proceed();前面的代码
调用这个函数pjp.proceed();
然后去执行这个拦截的方法
有异常执行这个异常通知
然后执行最终通知
注意:
Object object=pjp.proceed();后面的代码不会被执行

代码见附件。。。。。。。。。。。。。。。。。。。。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics