• spring511


    11、AOP

    11.3 使用Spring实现AOP

    【重点】使用AOP,需要导入一个依赖包!

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.8</version>
                <scope>runtime</scope>
            </dependency>
    

    目录结构:

    service包下的UserService接口:

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

    service包下的UserServiceImpl实现类:

    package com.kuang.service;
    
    public class UserServiceImpl implements UserService{
        public void add() {
            System.out.println("增加了一个用户");
        }
    
        public void delete() {
            System.out.println("删除了一个用户");
        }
    
        public void update() {
            System.out.println("修改了一个用户");
        }
    
        public void select() {
            System.out.println("查询了一个用户");
        }
    }
    

    log包下的Log类:

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

    log包下的AfterLog类:

    package com.kuang.log;
    
    
    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class AfterLog implements AfterReturningAdvice {
        //returnValue: 返回值
        public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
            System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
        }
    }
    

    diy包下的DitPointCut类:

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

    方式一:使用Spring的API接口【主要SpringAPI接口实现】
    对应的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: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
                http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--注册bean-->
        <bean id="userService" class="com.kuang.service.UserServiceImpl"/>
        <bean id="log" class="com.kuang.log.Log"/>
        <bean id="afterLog" class="com.kuang.log.AfterLog"/>
    
    <!--    方式一:使用原生的Spring API接口-->
        <!--配置aop:需要导入aop约束-->
        <aop:config>
            <!--切入点:-->
            <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
            <!--执行环绕增加!-->
            <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
            <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        </aop:config>
    
    </beans>
    

    方式二:自定义来实现AOP【主要是切面定义】
    对应的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: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
                http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--注册bean-->
        <bean id="userService" class="com.kuang.service.UserServiceImpl"/>
        <bean id="log" class="com.kuang.log.Log"/>
        <bean id="afterLog" class="com.kuang.log.AfterLog"/>
    
        <!--方式二:自定义类-->
        <bean id="diy" class="com.kuang.diy.DiyPointCut"/>
        <aop:config>
            <!--自定义切面,ref要引用的类-->
            <aop:aspect ref="diy">
                <!--切入点-->
                <aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
                <!--通知-->
                <aop:before method="before" pointcut-ref="point"/>
                <aop:after method="after" pointcut-ref="point"/>
            </aop:aspect>
        </aop:config>
    </beans>
    
  • 相关阅读:
    ASP.NET中常用的附件上传下载
    C#中导出Excel的常用方式
    ASP.NET中AjaxPro.dll的简单应用
    在ASP.NET中使用FusionCharts图表
    ASP.NET中使用MagicAjax.dll
    FusionCharts图表导出
    C#中经常注入的一些Javascript代码
    CodeSmith3.2(.net2.0)教程
    您未必知道的Css技巧
    Web Service简介
  • 原文地址:https://www.cnblogs.com/techgy/p/16092088.html
Copyright © 2020-2023  润新知