zoukankan      html  css  js  c++  java
  • 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的主要功能在日志,安全方面应用较多

     

  • 相关阅读:
    js关于页面坐标api
    js面向对象之创建对象
    Web前端安全问题
    js操作cookie
    css清除浮动
    块级格式化上下文(block formatting context)
    《python核心编程》笔记——系统限制
    《python核心编程》笔记——文件的创建、读取和显示
    黑客们的故事连载十二 “蠕虫来袭”:莫里斯
    黑客们的故事连载十一 下村努—虚拟战争追逐
  • 原文地址:https://www.cnblogs.com/springxian/p/13720843.html
Copyright © 2011-2022 走看看