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();
        }
    }
}
原文地址:https://www.cnblogs.com/zhshlimi/p/9676145.html