zoukankan      html  css  js  c++  java
  • spring切面拦截实现

    1.建立业务类(英雄战斗实现类)

    package com.g2.api;
    
    
    public interface Weapon {
        int attack();
        String getName();
    }
    package com.g2.api;
    
    import org.springframework.stereotype.Component;
    
    
    @Component
    public class Knifie implements Weapon {
        @Override
        public int attack() {
            System.out.println("温柔一刀");
            return 100;
        }
    
        @Override
        public String getName(){
            return "小李飞刀的刀";
        }
    }
    package com.g2.api;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    
    @Component
    public class Hero {
        private String name;
        private Weapon weapon;
    
        public Hero() {
    
        }
    
        public Hero(String name, Weapon weapon) {
            this.name = name;
            this.weapon = weapon;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Weapon getWeapon() {
            return weapon;
        }
    
        @Autowired
        public void setWeapon(Weapon weapon) {
            this.weapon = weapon;
        }
    
        public int fight() {
            System.out.println(String.format("英雄%s使用-%s在搏斗", name, weapon.getName()));
            return weapon.attack();
        }
    }

    2.建立切面配置类

    package com.g2.config;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    import org.springframework.stereotype.Component;
    
    
    //即使用jdk默认代理模式,AspectJ代理模式是CGLIB代理模式
    //如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP
    //如果目标对象实现了接口,可以强制使用CGLIB实现AOP (此例子我们就是强制使用cglib实现aop)
    //如果目标对象没有实现了接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换
    
    //用于定义配置类,可替换xml配置文件
    @Configuration
    //开启AspectJ 自动代理模式,如果不填proxyTargetClass=true,默认为false,
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    //扫描注入类
    @ComponentScan(basePackages = "com.g2.*")
    @Component
    @Aspect
    public class AopAspectConfiguration {
    
        @Around("execution(public * com.g2.api.Hero.fight(..))")
        public Object around(final ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("广告:英雄开始战斗了...");
            //处理前
    
            Object result= joinPoint.proceed();
          
            System.out.println(String.format("英雄完成了一次攻击"));
    
    
            return result;
        }
    }

    3.启动类

    package com.g2;
    
    import com.g2.api.Hero;
    import com.g2.config.AopAspectConfiguration;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class App {
        public static void main(String[] args) {
            try(AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
                    AopAspectConfiguration.class)) {
                Hero hero = applicationContext.getBean(Hero.class);
                hero.setName("陆小凤");
                hero.fight();
                hero.fight();
                hero.fight();
            }
        }
    }
  • 相关阅读:
    用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一:建立MAVEN Web项目
    jvm学习笔记二(减少GC开销的建议)
    jvm学习笔记一(垃圾回收算法)
    【转】 StringUtils中 isNotEmpty 和isNotBlank的区别
    单例模式
    工厂模式
    个人项目作业
    第一次作业-热身!
    第四单元总结
    第三单元总结
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/9676145.html
Copyright © 2011-2022 走看看