zoukankan      html  css  js  c++  java
  • spring Aop的一个demo

    面向切面是什么我就不说了.

    上代码:

    package com.foreveross.service.weixin.test;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Documented
    @Retention(value=RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface Action {
        String name();
    }
    package com.foreveross.service.weixin.test;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class DemoService {
    
        @Action(name="注解拦截操作...,这个是add操作...")
        public void addDemo(){
            
        }
    }
    package com.foreveross.service.weixin.test;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class Demo1Service {
        @Action(name="addDemo1的日志")
        public void addDemo1(){
            
        }
    }
    package com.foreveross.service.weixin.test;
    
    import java.lang.reflect.Method;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LogAspect {
    
        @Pointcut("@annotation(com.foreveross.service.weixin.test.Action)")
        public void annotationPointCut(){}
        
        @Before("execution(* com.foreveross.service.weixin.test.*.*(..))")
        public void before(JoinPoint joinPoint){
            MethodSignature signature=(MethodSignature)joinPoint.getSignature();
            Method method=signature.getMethod();
            System.out.println("方法规则式拦截:"+method.getName());
        }
        @After("annotationPointCut()")
        public void after(JoinPoint joinPoint){
            MethodSignature signature=(MethodSignature)joinPoint.getSignature();
            Method method=signature.getMethod();
            Action action=method.getAnnotation(Action.class);
            System.out.println("注解式拦截..."+action.name());
        }
    }
    package com.foreveross.service.weixin.test;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @ComponentScan("com.foreveross.service.weixin.test")
    @EnableAspectJAutoProxy//注解开启Spring对AspectJ的支持
    public class AppConfig {
    
    }
    package com.foreveross.service.weixin.test;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Test {
    
        
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);
            DemoService demo=context.getBean(DemoService.class);
            Demo1Service demo1=context.getBean(Demo1Service.class);
            demo.addDemo();
            demo1.addDemo1();
            context.close();
        }
    }
  • 相关阅读:
    探究Google力推的JetPack库<五>---------WorkManager
    探究Google力推的JetPack库<四>---------Navigation、Paging
    探究Google力推的JetPack库<一>---------Lifecycles、LiveData、ViewModel、手写LiveDataBus
    探究Google力推的JetPack库<二>---------用官方Data Binding来构建MVVM架构、集成Lifecycles完善MVP框架
    MVC->MVP->MVVM架构完整演变过程剖析
    返回多个值的摘要函数
    接受一个原子向量并返回一个列表的所有的函数
    简并碱基代码
    clustalX2使用以及相关的问题
    Word中不能加载EndNote怎么办
  • 原文地址:https://www.cnblogs.com/huzi007/p/6214697.html
Copyright © 2011-2022 走看看