zoukankan      html  css  js  c++  java
  • Spring AOP的简单示例

    配置文件

    <?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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            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-3.0.xsd">
    
        <!-- 激活组件扫描功能,在包cn.ysh.studio.spring.aop及其子包下面自动扫描通过注解配置的组件 -->
        <context:component-scan base-package="com.zhiguoguo.service,aspect"/>
    
        <!-- 激活自动代理功能 -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    </beans>


    service类:

    package com.zhiguoguo.service;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class HelloService {
        public String sayHello() {
            System.out.println("——————方法执行——————");
    //        throw new RuntimeException("haha");
            return "Hello world!";
        }
    }


    切面AOP:

    package com.zhiguoguo.aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    /**
     * 定义切面
     */
    @Component
    @Aspect
    public class HelloServiceAspect {
    
        @Before("execution(* com.zhiguoguo.service.HelloService.*(..))")
        public void authorith(){
            System.out.println("模拟进行权限检查。");
        }
    
        @After("execution(* com.zhiguoguo.service.HelloService.*(..))")
        public void release() {
            System.out.println("模拟方法结束后的释放资源...");
            System.out.println();
        }
    
        @AfterReturning(returning="obj", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))")
        public void log(Object obj) {
            System.out.println("模拟目标方法返回值:" + obj);
            System.out.println("模拟记录日志功能...");
            System.out.println();
        }
    
        @AfterThrowing(throwing="ex", pointcut="execution(* com.zhiguoguo.service.HelloService.*(..))")
        public void doRecoverActions(Throwable ex) {
            System.out.println("目标方法中抛出的异常:" + ex);
            System.out.println("模拟抛出异常后的增强处理...");
        }
    
    //    @Around("execution(* com.zhiguoguo.service.HelloService.*(..))")//这里先注释掉
        public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {
            System.out.println("执行目标方法之前,模拟开始事物...");
            // 执行目标方法,并保存目标方法执行后的返回值
            Object rvt = jp.proceed();
    
            //这里的切面方法如果有参数才能传递一个参数给他
    //        Object rvt = jp.proceed(new String[]{"被改变的参数"});
    
            System.out.println("执行目标方法之前,模拟结束事物...");
            System.out.println();
    
            //经过这么一个环绕,可以增强返回值
            return rvt + "新增的内容";
        }
    
    
    }


    主函数:

    package main;
    
    import com.zhiguoguo.service.HelloService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class HelloApp {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
            HelloService helloService = context.getBean(HelloService.class);
            helloService.sayHello();
        }
    }
  • 相关阅读:
    【职业规划】一位资深程序员大牛给予Java初学者的学习路线建议
    一个断点调试的小技巧
    无穷分数
    Spring事务异常回滚,捕获异常不抛出就不会回滚
    理解Servlet和Servlet容器、Web服务器等概念
    图解红黑树及Java进行红黑二叉树遍历的方法
    Majority Element
    Factorial Trailing Zeroes
    Valid Parentheses
    House Robber
  • 原文地址:https://www.cnblogs.com/wuyou/p/4123108.html
Copyright © 2011-2022 走看看