您的位置:首页 > 编程语言 > Java开发

Spring的AOP(面向切面编程)

2019-04-17 15:36 239 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_43984255/article/details/89356431
  1. 两种方式首先都要导入jar包
  • 使用xml配置的方式进行增强处理
      [code]<?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:p="http://www.springframework.org/schema/p"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd">
      
      <!-- 通过bean元素声明需要创建的对象实例,该实例的class属性表示类型,id属性表示表示该实例的名称(必须唯一) -->
      <!-- 创建实例 -->
      <bean id="student" class="com.pojo.Student">
      <!--property元素用来为实例属性赋值,这里调用的set方法赋值 name="属性名" value="属性值" -->
      <property name="stuName" value="张三"></property>
      </bean>
      
      <!-- 定义切面的bean -->
      <bean id="stuLogger" class="com.pojo.StuLogger"></bean>
      
      <!-- aop的增强处理 -->
      <aop:config>
      <!-- 定义切点expression="execution(需要增强的方法)" id="切点的名称" -->
      <aop:pointcut expression="execution(* com.pojo.*.*(..))" id="pt"></aop:pointcut>
      <!-- 各种增强 ref="引入切面的bean" -->
      <aop:aspect ref="stuLogger">
      <!-- 前置增强 method="实例中增强的方法名" pointcut-ref="引入需要增强的切点" -->
      <aop:before method="before" pointcut-ref="pt" />
      <!-- 后置增强 -->
      <aop:after method="after" pointcut-ref="pt" />
      <!-- 返回增强 returning="返回值"-->
      <aop:before-returning method="returning" returning="res" pointcut-ref="pt" />
      <!-- 异常增强 throwing="返回值" -->
      <aop:before-throwing method="throwing" throwing="e" pointcut-ref="pt" />
      <!-- 环绕增强 -->
      <aop:around method="around" pointcut-ref="pt" />
      </aop:aspect>
      </aop:config>
      </beans>

       

  • 使用@Aspect注解定义切面
      先在applicationContext.xml中配置
      [code]<?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:p="http://www.springframework.org/schema/p"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.2.xsd">
      
      <!-- 扫描包中注解标注的类,可以写多个包,用逗号隔开-->
      <context:component-scan base-package="com.pojo"></context:component-scan>
      <!-- 启动对@AspectJ注解的支持 -->
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
      </beans>
        
  • 使用注解定义一个需要增强的类
      [code]package com.pojo;
      
      import org.springframework.stereotype.Component;
      
      //使用注解定义了一个bean 相当于xml配置文件中<bean id="student" class="com.pojo.Student"></bean>
      @Component("student")
      public class Student {
      
      public void study(){
      System.out.println("学生正在学习");
      }
      
      public String work(){
      return "这是返回值";
      }
      
      public void exce(){
      throw new RuntimeException("出现异常了");
      }
      }

       

  • 使用@Aspect定义切面
      [code]package com.pojo;
      
      import org.aspectj.lang.JoinPoint;
      import org.aspectj.lang.annotation.After;
      import org.aspectj.lang.annotation.AfterReturning;
      import org.aspectj.lang.annotation.AfterThrowing;
      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 StuLogger {
      
      //单独定义一个切入点可以共用
      @Pointcut("execution(public void study())")
      public void aa(){
      
      }
      
      //前置增强
      @Before("aa()")
      public void before(JoinPoint jp){
      System.out.println("目标对象:"+jp.getTarget()+",方法:"+jp.getSignature().getName()+"--进行前置增强-----");
      }
      //后置增强
      @After("aa()")
      public void after(JoinPoint jp){
      System.out.println("目标对象:"+jp.getTarget()+",方法:"+jp.getSignature().getName()+"--进行后置增强-----");
      }
      //返回增强
      @AfterReturning(value="execution(public String work())",returning="res")
      public void returning(JoinPoint jp,Object res){
      System.out.println("目标对象:"+jp.getTarget()+",方法:"+jp.getSignature().getName()+"--进行返回增强-----返回值是:"+res);
      }
      //异常增强
      @AfterThrowing(value="execution(public void exce())",throwing="e")
      public void throwing(JoinPoint jp,Exception e){
      System.out.println("目标对象:"+jp.getTarget()+",方法:"+jp.getSignature().getName()+"--进行异常增强-----异常:"+e);
      }
      }

       

  • 使用Spring测试
      [code]package com.test;
      
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      import com.pojo.Student;
      
      public class SpringAopTest {
      public static void main(String[] args) {
      ApplicationContext	ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
      Student s1 =(Student) ctx.getBean("student");
      s1.study();//根据..调方法
      }
      }
  • 内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: