zoukankan      html  css  js  c++  java
  • Spring入门学习(六)

    AOP

    1、AOP简介

    AOP(Aspect Oriented Programming):面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

    2、AOP在Spring中的作用

    提供声明式事务;允许用户自定义切面

    • 横切关注点:跨越应用程序多个模块的方法或功能。即是与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志、安全、缓存、事务等等...
    • 切面(ASPECT):横切关注点被模块化的特殊对象。即是一个类;
    • 通知(Advice):切面必须要完成的工作。即他是类中的一个方法;
    • 目标(Target):被通知对象
    • 代理(Proxy):向目标对象应用通知之后创建的对象
    • 切入点(PointCut):切面通知执行"地点"的定义;
    • 连接点(JointPoint):与切入点匹配的执行点。

    SpringAOP中,通过Advice定义横切逻辑,Spring中支持5中类型的Advice

    即AOP在不改变原有代码的情况下,去增加新的功能。

    3、使用Spring实现AOP

    使用AOP织入,需要导入一个依赖包!

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>
    

    方式一:使用SpringAPI接口【主要SpringAPI接口实现】

    1、编写接口

    package com.star.service;
    
    public interface UserService {
        public void add();
    
        public void delete();
    
        public void update();
    
        public void search();
    }
    

    2、接口的实现类

    package com.star.service.impl;
    
    import com.star.service.UserService;
    
    public class UserServiceImpl implements UserService {
        public void add() {
            System.out.println("add");
        }
    
        public void delete() {
            System.out.println("delete");
        }
    
        public void update() {
            System.out.println("update");
        }
    
        public void search() {
            System.out.println("select");
        }
    }
    

    3、前置日志

    package com.star.config;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class LogBefore implements MethodBeforeAdvice {
    
        //method:要执行的目标对象的方法
        //args:参数
        //target:目标对象
        public void before(Method method, Object[] args, Object target) throws Throwable {
            System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
        }
    }
    

    4、后置日志

    package com.star.config;
    
    import org.springframework.aop.AfterReturningAdvice;
    import java.lang.reflect.Method;
    
    public class LogAfter implements AfterReturningAdvice {
    
        public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
            System.out.println("执行了"+method.getName()+"方法并返回了"+returnValue);
        }
    }
    

    5、配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--方式一:使用原生Spring API接口-->
        <!--注册bean-->
        <bean id="userService" class="com.star.service.impl.UserServiceImpl"/>
        <bean id="LogAfter" class="com.star.config.LogAfter"/>
        <bean id="LogBefore" class="com.star.config.LogBefore"/>
        <!--需要导入aop的约束-->
        <aop:config>
            <!--切入点:expression:表达式,execution(要执行的位置!) *(..):其中 * 表示所有的方法 (..)表示任何参数-->
            <aop:pointcut id="pc-userService" expression="execution(* com.star.service.impl.UserServiceImpl.*(..))"/>
    
            <!--执行环绕增强-->
            <aop:advisor advice-ref="LogBefore"  pointcut-ref="pc-userService"/>
            <aop:advisor advice-ref="LogAfter"  pointcut-ref="pc-userService"/>
        </aop:config>
    
    </beans>
    

    6、测试类

    import com.star.service.UserService;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
    
        @Test
        public void UserService(){
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService userService = (UserService) context.getBean("userService");
            userService.add();
        }
    }
    

    测试结果:

    可以看到前置日志和后置日志添加成功!

    方式二:自定义来实现AOP【主要切面定义】

    1、自定义类

    package com.star.config;
    
    public class diy {
    
        public void before(){
            System.out.println("=========方法执行前=========");
        }
    
        public void after(){
            System.out.println("=========方法执行后=========");
        }
    
    }
    

    2、配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--方式二:使用自定以类-->
        <!--注册bean-->
        <bean id="diy" class="com.star.config.diy"/>
        <bean id="userService" class="com.star.service.impl.UserServiceImpl"/>
        <aop:config>
            <!--自定义切面:类   ref:要引用的类-->
            <aop:aspect ref="diy">
                <!--切入点-->
                <aop:pointcut id="pc-userService" expression="execution(* com.star.service.impl.UserServiceImpl.*(..))"/>
                <!--通知-->
                <aop:before method="before" pointcut-ref="pc-userService"/>
                <aop:after method="after" pointcut-ref="pc-userService"/>
            </aop:aspect>
        </aop:config>
    </beans>
    

    测试类不变!

    测试结果:

    方式三:使用注解实现!

    1、自定以类,使用注解

    package com.star.config;
    
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    
    @Aspect //标注这个类是一个切面
    public class Annotation {
    
        //注意导包
        @Before("execution(* com.star.service.impl.UserServiceImpl.*(..))")
        public void before(){
            System.out.println("=========方法执行前=========");
        }
    
        @After("execution(* com.star.service.impl.UserServiceImpl.*(..))")
        public void after(){
            System.out.println("=========方法执行后=========");
        }
    }
    

    2、配置文件(或者在自定义类上添加注解@Component)

        <!--方式三 注解-->
        <!--注册bean-->
        <bean id="annotation" class="com.star.config.Annotation"/>
        <bean id="userService" class="com.star.service.impl.UserServiceImpl"/>
        <!--开启注解支持  JDK(默认)proxy-target-class="true"   cglib proxy-target-class="true"-->
        <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>
    

    测试类不变

    测试结果:

  • 相关阅读:
    造数--存储过程,循环
    一句话搞定python六剑客
    数据库基本操作--增删改查
    session与cookie区别与联系
    正则表达式中 group groups区别
    迭代总结(随时更新)
    postman抓包
    linux常用命令
    抓包
    jdk 11特性
  • 原文地址:https://www.cnblogs.com/lmx-181028/p/12433755.html
Copyright © 2011-2022 走看看