Spring的AOP(面向切面编程)

  1. 两种方式首先都要导入jar包
  2. 使用xml配置的方式进行增强处理
    1. <?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>

       

  3. 使用@Aspect注解定义切面
    1. 先在applicationContext.xml中配置
      1. <?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>
          
    2. 使用注解定义一个需要增强的类
      1. 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("出现异常了");
        	}
        }
        

         

    3. 使用@Aspect定义切面
      1. 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);
        	}
        }
        

         

    4. 使用Spring测试
      1. 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();//根据..调方法
        	}
        }
        
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值