美文网首页
spring-AOP

spring-AOP

作者: 李霖神谷 | 来源:发表于2019-12-09 23:25 被阅读0次

普通的oop工程,方法都是纵向的依次执行的,而aop是当方法执行的时候,执行前后添加额外的一些功能,这种纵向的执行流程称之为面向切面编程(aop)。
这里介绍几个概念:
切点:pointcut,指的是原有的执行方法
前置通知:在切点之前进行的操作befor advice
后置通知:在切点之后进行的操作after advice
切点执行过程中出现异常会触发异常通知throws advice
所有的这些功能叫做切面
织入:把切面织入到原有的功能上叫做织入
代码实现,当一个方法被调用的时候,执行前后分别由林外的操作
前置通知,后置通知类:

package com.shuai.advice;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterAdvice implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("我先得洗澡,擦润肤乳");
    }
}
package com.shuai.advice;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforAdvice implements MethodBeforeAdvice {
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("我要先玩一局王者");
    }
}

原有的功能类:

package com.shuai.domain;

public class people {
    public  void study(){
        System.out.println("我要学习了");
    }
    public  void sleep(){
        System.out.println("我要睡觉了");
    }
}

xml配置:execution(* com.shuai.domain.people.study())星后要有空格

<?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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置切点-->
    <aop:config>
        <aop:pointcut id="peopleCut" expression="execution(* com.shuai.domain.people.study())" ></aop:pointcut>
        <aop:advisor advice-ref="beforAdvice" pointcut-ref="peopleCut"></aop:advisor>
        <aop:advisor advice-ref="afterAdvice" pointcut-ref="peopleCut"></aop:advisor>
    </aop:config>
    <!--指定前置通知-->
    <bean id="beforAdvice" class="com.shuai.advice.BeforAdvice"></bean>
    <!--指定后置通知-->
    <bean id="afterAdvice" class="com.shuai.advice.AfterAdvice"></bean>
    <bean id="people" class="com.shuai.domain.people"></bean>
</beans>

测试:

import com.shuai.domain.people;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
        people p= applicationContext.getBean("people", people.class);
        p.study();
        p.sleep();
    }
}

异常通知:


    <aop:config>
        <aop:aspect ref="advice">
            <aop:pointcut id="peopleCut" expression="execution(* com.shuai.domain.people.study())"></aop:pointcut>
            <aop:after-throwing method="mytestException" pointcut-ref="peopleCut"></aop:after-throwing>
        </aop:aspect>
    </aop:config>
    <bean id="people" class="com.shuai.domain.people"></bean>
    <bean id="advice" class="com.shuai.advice.ExceptionAdvice"></bean>

注解配置aop

package com.shuai.domain;

import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
public class people {
    @Pointcut("execution(* com.shuai.domain.people.study())")
    public  void study(){

        System.out.println("我要学习了");
    }
    public  void sleep(){
        System.out.println("我要睡觉了");
    }

}
package com.shuai.advice;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class BeforAdvice {
    @Before("com.shuai.domain.people.study()")
    public void before()  {
        System.out.println("我要先玩一局王者");
    }
}

xml配置:

    <context:component-scan base-package="com.shuai.domain,com.shuai.advice"></context:component-scan>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>

   ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
        people p= applicationContext.getBean("people", people.class);
        p.study();

相关文章

  • 自定义注解,aop+redis,实现controller接口频率

    1,环境配置 引入aop的jar包compile 'org.springframework:spring-aop:...

  • spring-4.3.4.RELEASE集成AOP 实战

    一、依赖引入 (包括 spring-aop 以及 aspectj) 二、切面配置代码 (使用 javaConfig...

  • spring-AOP

    Aspects,切面 spring-aop 是spring-aspects的上层建筑 targetClass Me...

  • spring-aop

    aop概念aop概念aop术语AOP实现方式1、spring-aop(使用xml文件实现AOP)2、AspectJ...

  • spring-aop

    Aop-面向切面编程,在程序中主要用来解决一些系统层面上的问题,比如:日志,事务,权限等。 aop的一些基本概念:...

  • spring-aop

    1, aop的两种实现机制动态代理:利用jdk/cglib动态代理,性能弱一丢丢 jdk动态代理:所有的方法调用被...

  • spring-aop

  • Spring-AOP

    AOP的概念 面向切面的编程,切面用于描述分散在对象、类或者函数中的横向关注点,通过分离这些关注点,把解决特定领域...

  • spring-aop

    aop概念 aop术语 joinpoint连接点:类中可以被增强的方法(其实就是类中的方法) pointcut切入...

  • spring-aop

    aop 概念:面向切面编程作用:不用修改原来的类就可以添加新功能 专业术语 joinpoint 连接点:类中可以被...

网友评论

      本文标题:spring-AOP

      本文链接:https://www.haomeiwen.com/subject/algigctx.html