• Spring--->AOP


    第一种

    1、User Service接口

    package com.xian.dao;
    
    public interface UserService {
        public void add();
        public void delete();
        public void insert();
        public void update();
    
    }

    2、接口实现类(重写全部的方法)

    package com.xian.service;
    
    import com.xian.dao.UserService;
    
    public class UserServiceImpl implements UserService {
    
        public void add() {
            System.out.println("增加用户");
    
        }
    
        public void delete() {
            System.out.println("删除用户");
    
        }
    
        public void insert() {
            System.out.println("插入用户");
    
        }
    
        public void update() {
            System.out.println("更新用户");
    
        }
    }

    3、前部环绕

    package com.xian.service;
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class Log implements MethodBeforeAdvice {
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("执行了"+o.getClass().getName()+"的"+method.getName()+"方法");
        }
    }

    4、后部环绕

    package com.xian.service;
    
    import org.springframework.aop.AfterReturningAdvice;
    
    import java.lang.reflect.Method;
    
    public class AfterLog implements AfterReturningAdvice {
        public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {
            System.out.println("执行了"+target.getClass().getName()+"的"+method.getName()+"方法"+"返回值:"+returnValue);
        }
    }

    5、配置Spring-beans.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 id="log" class="com.xian.service.Log"></bean>
        <bean id="afterLog" class="com.xian.service.AfterLog"/>
        <bean id="userService" class="com.xian.service.UserServiceImpl"/>
        <aop:config>
            <aop:pointcut id="pointcut" expression="execution(* com.xian.service.UserServiceImpl.*(..))"/>
            <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
            <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
        </aop:config>
    </beans>

    6、test

    import com.xian.dao.UserService;
    import com.xian.service.UserServiceImpl;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        @org.junit.Test
        public void Test01(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            UserService userservice = (UserService) context.getBean("userService");
            userservice.add();
        }
    }

    第二种 自定义

    上面User Service和UserServiceImpl不变

    1、编写 环绕要执行的代码

    public class DiyPoint {
        public void before(){
            System.out.println("---before---");
        }
        public void after(){
            System.out.println("---after---");
        }
    }

    2、配置Bean

    <?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 id="diy" class="com.xian.dao.DiyPoint"/>
        <bean id="userService" class="com.xian.service.UserServiceImpl"/>
    
        <!--aop的配置-->
        <aop:config>
            <!--第二种方式:使用AOP的标签实现-->
           <aop:aspect ref="diy">
               <aop:pointcut id="diyPointCut" expression="execution(* com.xian.service.UserServiceImpl.*(..))"/>
               <aop:before pointcut-ref="diyPointCut" method="before"></aop:before>
               <aop:after pointcut-ref="diyPointCut" method="after"></aop:after>
           </aop:aspect>
        </aop:config>
    </beans>

    3、Test

    public class Test {
        @org.junit.Test
        public void Test01(){
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            UserService userservice = (UserService) context.getBean("userService");
            userservice.add();
        }
    }

    第二种明显没有第一种强

    第三种 注解开发

    之前的还是不变,直接配置注解

    1、首先需要环绕的类

    @Aspect
    public class Annotation {
        @Before("execution(* com.xian.service.UserServiceImpl.*(..))")
        public void before(){
            System.out.println("---before---");
        }
        @After("execution(* com.xian.service.UserServiceImpl.*(..))")
        public void after(){
            System.out.println("---after---");
        }
        @Around("execution(* com.xian.service.UserServiceImpl.*(..))")
        public void arround(ProceedingJoinPoint jp) throws Throwable {
            System.out.println("环绕前");
            System.out.println("签名前:"+jp.getSignature());
            Object process=jp.proceed();
            System.out.println("环绕后");
            System.out.println(process);
        }
    }

    2、配置Bean

    <bean id="annotationPointcut" class="com.kuang.config.AnnotationPointcut"/>
    <aop:aspectj-autoproxy/>

    test就完事了

    AOP的主要功能在日志,安全方面应用较多

     

  • 相关阅读:
    教你发布Silverlight Bussiness Application(SQL Server 登录,局域网访问,以及使用ArcGIS Server服务需要注意的问题)
    ArcGIS API for Silverlight 使用GeometryService进行河流网格划分(三)
    ArcGIS Server 10.1发布要素服务时遇到的数据库注册问题总结 (二)
    使用ArcGIS API for Silverlight + Visifire绘制地图统计图
    使用Visifire+ArcGIS API for Silverlight实现Graphic信息的动态图表显示
    报个道
    Cgroups
    docker的文件系统
    go实现http服务
    linux调度器原理
  • 原文地址:https://www.cnblogs.com/springxian/p/13720843.html
Copyright © 2020-2023  润新知