zoukankan      html  css  js  c++  java
  • spring详解(三)——AOP

    AOP: 注解来完成

    1、加入依赖的jar文件

    2、创建一个切面类

    package com.zhiyou.zjc.aspect;
    
    import java.util.Arrays;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    
    @Component
    @Aspect
    public class CountAspect {    
        @Before("execution(* com.zhiyou.zjc.aspect.CountDaoImp.*(..))")
        public void logbefore(JoinPoint joinPoint) {
            Object[] args = joinPoint.getArgs();
            System.out.println("==========before=========="+joinPoint.getSignature().getName()+Arrays.asList(args));
        }
        @After("execution(* com.zhiyou.zjc.aspect.CountDaoImp.*(..))")
        public void logafter() {
            System.out.println("=====after=======");
        }
        
    }

    3、在spring配置文件中开启切面注解

    <context:component-scan base-package="com.zhiyou.zjc.aspect"></context:component-scan>
    <aop:aspectj-autoproxy/>

    4、测试

    package com.zhiyou.zjc.aspect;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext app = new ClassPathXmlApplicationContext("spring.xml");
            CountDao coImp = (CountDao) app.getBean("imp2");
            coImp.add(2, 3);
        }  
        
    }

    AOP:基于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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
        <!-- 定义被通知的程序类 -->
        <bean id="imp2" class="com.zhiyou.zjc.aspect.CountDaoImp2"></bean>
        <!-- 定义切面类的bean -->
        <bean id="aspect" class="com.zhiyou.zjc.aspect.CountAspect2" />
        
        <!-- 配置切面的xml文件 -->
        <aop:config>
            <!-- 定义表达式切点 -->
            <aop:pointcut expression="execution(* com.zhiyou.zjc.aspect.*.*(..))" id="pointcut"/>    
            <!-- 定义切面 -->    
            <aop:aspect id="lodaspect" ref="aspect">
                <!-- 定义前置通知 -->
                <aop:before method="logbefore" pointcut-ref="pointcut"/>
                <!-- 定义后置通知 -->
                <aop:after method="logafter" pointcut-ref="pointcut"/>
            </aop:aspect>
            
            
        </aop:config>
    </beans>
  • 相关阅读:
    Change MYSQL data directory
    Docker distrubution in django
    mongo&node
    理解Docker单机容器网络
    auto dock
    django_restframework_angularjs
    Javascript异步编程的4种方法
    DockerProblem
    Javascript面向对象编程
    scrapy post请求 multipart/form-data
  • 原文地址:https://www.cnblogs.com/zjc364259451/p/11503356.html
Copyright © 2011-2022 走看看