码迷,mamicode.com
首页 > 编程语言 > 详细

spring的AOP(Aspect Oriented Programming)面向切面编程

时间:2019-09-09 23:02:09      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:throw   frame   return   技术   ali   定义   bean   面向   value   

程序中需要有日志等需要,若在原本程序加代码会导致代码混乱,不宜维护,解决方法:

  1. 使用代理。
  2. 使用spring的AOP。

 

spring的AOP注解方式使用:

1.加入jar包(com.springsource.org.aopalliance,sapectj.weaver,spring-aop)

 技术图片

 

 

2.创建一个切面类(Aspect)

package com.zhiyou100.kfs.aspects;

 

import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

 

@Aspect //切面

@Component       //bean

public class LogAspects {

       /*

        *   第一个*:通配符  任意访问修饰符和任意返回值

        *   第二个*:类

        *   第三个*:方法

        *   ..:可变参数

        */

       @Before(value="execution(* com.zhiyou100.kfs.aspects.*.*(..))")

       public void before(JoinPoint joinpoint) {

              Object[] args = joinpoint.getArgs();//参数

              String name = joinpoint.getSignature().getName();//方法名字

              System.out.println("begin,method:"+name);

       }

       @AfterReturning

       @After(value="execution(* com.zhiyou100.kfs.aspects.*.*(..))")       //方法执行return前执行

       public void after(JoinPoint joinpoint) {

              String name = joinpoint.getSignature().getName();//方法名字

              System.out.println("end,method:"+name+",result:");

       }

}

 

3.在spring配置文件开启切面注解

       <!-- 包扫描 -->

       <context:component-scan base-package="com.zhiyou100.kfs"/>

       <!-- 开启切面注解 -->

       <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

4.测试代码

                   ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");

                   TestProxy t=(TestProxy)app.getBean("testProxyImp");

                   t.add(1, 8,1);

 

spring的AOPxml方式使用:

1.创建一个需要被通知的接口和相应实现类

接口:

package com.zhiyou100.kfs.aspects.xml;

 

public interface TestProxy {

       public void add(int a,int b);

       public int add(int a,int b,int c);

}

相应实现类:

package com.zhiyou100.kfs.aspects.xml;

 

public class TestProxyImp implements TestProxy  {

      

       public void add(int a,int b){

              int sum=a+b;

              System.out.println(sum);

              throw new RuntimeException("运行时异常");

       }

      

       public int add(int a,int b,int c){

              int sum=a+b+c;

              System.out.println(sum);

              return sum;

       }

}

2.在spring配置文件中配置。

<?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:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

 

       <!-- 定义被通知的程序类(可以用注解) -->

       <bean id="testProxyImp" class="com.zhiyou100.kfs.aspects.xml.TestProxyImp"></bean>

       <!-- 定义切面类的bean -->

       <bean id="logAspect" class="com.zhiyou100.kfs.aspects.xml.LogAspects"></bean>

       <!-- 配置切面 -->

       <aop:config>

              <!-- 定义表达式:切点 -->

              <aop:pointcut expression="execution(* com.zhiyou100.kfs.aspects.xml.*.*(..))" id="pointcut"/>

              <!-- 定义切面 -->

              <aop:aspect ref="logAspect">

                     <!-- 定义前置通知 -->

                     <aop:before method="before" pointcut-ref="pointcut"/>

                     <!-- 定义后置通知 -->

                     <aop:after method="after" pointcut-ref="pointcut"/>

                     <!-- 定义返回通知 -->

                     <aop:after-returning method="cc" pointcut-ref="pointcut" returning="result"/>

                     <!-- 定义异常通知 -->

                     <aop:after-throwing method="e" pointcut-ref="pointcut" throwing="e"/>

              </aop:aspect>

       </aop:config>

</beans>

3.最后去测试

spring的AOP(Aspect Oriented Programming)面向切面编程

标签:throw   frame   return   技术   ali   定义   bean   面向   value   

原文地址:https://www.cnblogs.com/kfsrex/p/11494621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!