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();
}
}
}

spring切面拦截实现的更多相关文章

  1. 从零开始学 Java - Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

  2. Java - Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

  3. Spring使用拦截器支持国际化(转)

    Spring使用拦截器支持国际化很方便,使用时只需要两个步骤: 一.spring配置 具体配置方式如下: <!-- 资源文件绑定器,文件名称:messages.properties(没有找到时的 ...

  4. Spring 中拦截器与过滤器的区别

    spring 中拦截器 与servlet 的filter 有相似之处.比如二者都是aop 编程思想的体现都能实现权限检查,日志记录等. 不同之处 使用范围不同 Filter 是Servlet 规定的. ...

  5. spring MVC拦截器01

    spring MVC拦截 作用:身份校验,权限检查,防止非法訪问. 场景:一个bbs系统,用户没有登录就无法发帖或者删除评论; 一个博客系统,没有登录就无法发表博文,无法添加分类,无法删除博文. sp ...

  6. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

  7. spring mvc拦截器原理分析

    我的springMVC+mybatis中的interceptor使用@autowired注入DAO失败,导致报空指针错误,这个是为什么呢? :空指针说明没有注入进来,你可以检查一下你的这个拦截器int ...

  8. SSM(spring mvc+spring+mybatis)学习路径——2-2、spring MVC拦截器

    目录 2-2 Spring MVC拦截器 第一章 概述 第二章 Spring mvc拦截器的实现 2-1 拦截器的工作原理 2-2 拦截器的实现 2-3 拦截器的方法介绍 2-4 多个拦截器应用 2- ...

  9. Spring 注解拦截器使用详解

    Spring mvc拦截器 平时用到的拦截器通常都是xml的配置方式.今天就特地研究了一下注解方式的拦截器. 配置Spring环境这里就不做详细介绍.本文主要介绍在Spring下,基于注解方式的拦截器 ...

随机推荐

  1. CodeForces 342B Xenia and Spies (水题模拟,贪心)

    题意:给定 n 个间谍,m个区间,一个 s,一个f,然后从 s开始传纸条,然后传到 f,然后在每个 t 时间在区间内的不能传,问你最少的时间传过去. 析:这个题,就模拟一下就好,贪心策略,能传就传,找 ...

  2. 8) Struts2 2 SpringMVC

    git@github.com:witaste/smse.git 数据库脚本: /* Navicat MySQL Data Transfer Source Server : 新服务器 Source Se ...

  3. PHP中文乱码解决办法[转]

    一.首先是PHP网页的编码1.     php文件本身的编码与网页的编码应匹配a.     如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/htm ...

  4. Locality preserving hashing for fast image search: theory and applications

    Is there any Java library that provides an implementation (or several) of a Locality Preserving Hash ...

  5. 设计模式13---桥接模式(Bridge Pattern)

    桥接模式将抽象与具体实现分离,使得抽象与具体实现可以各自改变互不影响.桥接模式属于设计模式中的结构模式. 桥梁模式涉及的角色 抽象(Abstraction)角色:抽象定义,引用对接口对象的引用. 重新 ...

  6. auc的本质

    AUC的本质 定义 auc是roc曲线下的面积.其中,roc是横坐标为fpr,纵坐标是tpr的坐标系上的曲线. TPR(true positive rate):所有正样本中被预测为正的比例 FPR(f ...

  7. Cordova deploy on Android

    网上找了几篇Phonegap在安卓上的部署,版本都比较老了,不过还是部署成功了, 写篇博客以做纪录. 1.先下载IDE:戳 2.下载Phonegap:戳 3.启动ADT,新建普通Android App ...

  8. GridView中合并单元格

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  9. Filter 设计模式编码实践

    原文地址: haifeiWu和他朋友们的博客 博客地址:www.hchstudio.cn 欢迎转载,转载请注明作者及出处,谢谢! 最近项目中遇到各种输出数据监控,数据校验等逻辑,一个个实现很是麻烦.项 ...

  10. JComboBox组件的列表实时更新

    JComboBox deleteUser = new JComboBox(queryUser()); //queryUser()的返回值为字符串数组 deleteUser.setModel(new D ...