zoukankan      html  css  js  c++  java
  • spring的APO注解方式

    一、配置

      1、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"
           xmlns:context="http://www.springframework.org/schema/context"
           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
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.wuxi"></context:component-scan>
    
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
    </beans>

      2、配置类方式

    package com.wuxi.configs;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @ComponentScan("com.wuxi")
    @EnableAspectJAutoProxy
    public class SpringConfig {
    }

    二、增强的功能所在的类(通知类)

    package com.wuxi.utils;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    /*
    @Before、@AfterReturning、@AfterThrowing、@After会有顺序问题(最终通知会在异常或后置通知之前执行),
    使用@Around可以避免顺序问题
    */
    @Component
    @Aspect
    public class LoggerService {
    
        @Pointcut("execution(public void com.wuxi.services.impl.StudentServiceImpl.removeStudent())")
        private void pt() {
        }
    
        @Before("pt()")
        public void printLog() {
            System.out.println("打印日志");
        }
    
        @Around("execution(public String com.wuxi.services.impl.StudentServiceImpl.findStudent())")
        public Object printAroundLog(ProceedingJoinPoint pjp) {
            Object proceed = null;
            try {
                System.out.println("环绕前置通知");
                Object[] args = pjp.getArgs();
                proceed = pjp.proceed(args);
                System.out.println("环绕后置通知");
            } catch (Throwable throwable) {
                System.out.println("环绕异常通知");
                throwable.printStackTrace();
            } finally {
                System.out.println("环绕最终通知");
            }
            return proceed;
        }
    }

    三、测试

    package com.wuxi.tests;
    
    import com.wuxi.configs.SpringConfig;
    import com.wuxi.services.StudentService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MySpringTest {
        public static void main(String[] args) {
            // ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
            ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
    
            StudentService studentService = (StudentService) ac.getBean("studentServiceImpl");
            // studentService.removeStudent();
            studentService.findStudent();
        }
    }
  • 相关阅读:
    Nginx 和 PHP的安装配置
    hdu1166 敌兵布阵
    乘法逆元详解
    SPFA算法
    Kruskal算法&Prim算法
    WC2018 文艺汇演《退役的你》
    HDU2577 How to Type
    裴蜀定理的证明
    CSP J/S 2019受虐记
    Dijkstra算法详解
  • 原文地址:https://www.cnblogs.com/linding/p/13667143.html
Copyright © 2011-2022 走看看