AOP简单配置

7 篇文章 0 订阅

1.AOP,面向切面(方面)编程,扩展功能不修改源代码实现。采用横向抽取机制实现扩展,取代了传统纵向继承体系重复性代码。
2.AOP操作术语:
->Joinpoint(连接点):类里面可以被增强的方法,成为连接点;
->Pointcut(切入点):类中可以有很多方法被增强,比如实际操作中,只是增强了类里面的add和update方法,实际增强的方法被称为切入点;
->Advice(通知/增强):增强的逻辑,成为增强,比如扩展日志功能,这个日志功能成为增强;
***前置通知:在方法之前执行
***后置通知:在方法之后执行
***异常通知:在方法出现异常执行
***最终通知:在后置之后执行
***环绕通知:在方法之前或者方法之后都执行
->Aspect(切面):把增强应用到具体方法上面,即把增强用到切入点的过程称为切面
->Introduction(引介):在类中动态添加属性和方法;
->Target(目标对象):增强方法所在的类称为目标对象;
->Weaving(织入):把增强应用到目标对象的过程称为织入;
->Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类;
3.简单实现
->导入依赖

<!--spring核心jar-->
	<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.10.RELEASE</version>
	</dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    
     <!-- 实现AOP需要加入的jar包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.8.0</version>
    </dependency>
    <dependency>
       <groupId>aopalliance</groupId>
       <artifactId>aopalliance</artifactId>
       <version>1.0</version>
     </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-aspects</artifactId>
       <version>4.3.10.RELEASE</version>
     </dependency>

->创建配置文件(applicationContext.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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 	<!--context (注解)    aop(面向切面)    tx(事务)-->
 	
    <!--开启注解扫描-->
    <context:component-scan base-package="com.mvc"/>

    <!--配置AOP-->
    <aop:config>
       <!-- 配置切入点,对show()方法进行增强-->
        <aop:pointcut id="p1" expression="execution(* com.mvc.pojo.Student.show(..))"/>
        <!--配置切面-->
        <aop:aspect ref="studentUp">
            <!--配置增强类型 method,增强前置-->
            <aop:before method="before" pointcut-ref="p1"/>
            <!--配置增强类型 method,后置前置-->
            <aop:after method="after" pointcut-ref="p1"/>
            <!--配置增强类型 method,环绕增强-->
            <aop:around method="around" pointcut-ref="p1"/>
        </aop:aspect>
    </aop:config>
</beans>

->目标类中方法

@Component
public class Student {
    public void show(){
        System.out.println("Student.show()");
    }
}

->增强方法的类

@Component
public class StudentUp {
    public void before(){
        System.out.println("前置增强");
    }
    public void after(){
        System.out.println("后置增强");
    }
    public void around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("方法执行前环绕");
        Object obj = pjp.proceed();
        System.out.println("方法执行前环绕");
    }
}

->测试类

@Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        student.show();
    }

->结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值