• Spring-增强方式注解实现方式


    SpringAOP增强是什么,不知道的到上一章去找,这里直接上注解实现的代码(不是纯注解,纯注解后续会有)

    创建业务类代码

     1 @Service("dosome")//与配置文件中<bean id="dosome" class="com.com.service.DoSome"/>一样
     2 public class DoSome {
     3     public void dosome(){
     4         System.out.println("我是service层");
     5     }
     6     //该方法为了对比最终增强和后置增强的效果,还有就是异常抛出增强的效果
     7     public void error(){
     8         int result=5/0;//伪造一个异常
     9         System.out.println("我是异常方法");
    10     }
    11 }

    创建通知类代码

     1 @Aspect//标注这个类是一个切面
     2 @Component//作用与@Service作用相同
     3 public class Advice implements AfterAdvice {
     4     //标注一个切点,这个方法只是为了写一个表达式,什么都不用做
     5     @Pointcut("execution(* com.cn.service.*.*(..))")
     6     public void pointcut(){}
     7     //前置增强
     8     @Before("pointcut()")
     9     public void before(JoinPoint jp){
    10         System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
    11     }
    12     //后置增强
    13     @AfterReturning(value = "pointcut()",returning = "obj")
    14     public void afterReturning(JoinPoint jp,Object obj){
    15         System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
    16     }
    17     //环绕增强
    18     @Around("pointcut()")
    19     public void around(ProceedingJoinPoint jp) throws Throwable {
    20         System.out.println("我是环绕增强中的前置增强!");
    21         Object proceed = jp.proceed();//植入目标方法
    22         System.out.println("我是环绕增强中的后置增强!");
    23     }
    24     //异常抛出增强
    25     @AfterThrowing(value = "pointcut()",throwing = "e")
    26     public void error(JoinPoint jp,Exception e){
    27         System.out.println("我是异常抛出增强"+",异常为:"+e.getMessage());
    28     }
    29     //最终增强
    30     @After("pointcut()")
    31     public void after(JoinPoint jp){
    32         System.out.println("我是最终增强");
    33     }
    34 }

    核心配置文件applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     6     <!--包扫描-->
     7     <context:component-scan base-package="com.cn"/>
     8     <!--开启ioc注解-->
     9     <context:annotation-config/>
    10     <!--开启aop注解-->
    11     <aop:aspectj-autoproxy/>
    12 </beans>

    编写测试类代码

     1 public class App 
     2 {
     3     public static void main( String[] args )
     4     {
     5         ApplicationContext ac=new ClassPathXmlApplicationContext("/applicationContext.xml");
     6         DoSome bean = ac.getBean("dosome",DoSome.class);
     7         bean.dosome();
     8         //bean.error();
     9     }
    10 }

    后续补充纯注解配置方式,更方便一些

  • 相关阅读:
    this指针详解
    C++处理异常
    C++中的this指针
    c++中的string类
    c面试题总结
    c++中的引用详解
    c++中的new和delete
    函数重载
    BST(二叉排序树)的插入与删除
    ccf行车路线
  • 原文地址:https://www.cnblogs.com/Tiandaochouqin1/p/10423980.html
Copyright © 2020-2023  润新知