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. docker镜像存出与载入

    尝试从官网上下载ubuntu镜像,太慢下载不下来. 使用daocloud加速器进行加速之后,由于公司网络不好,仍然下载不下来. 没办法,只能从别的环境上搞一个已经存在的ubuntu镜像,折腾到自己的虚 ...

  2. Centos7下安装与卸载Jdk1.8

    安装 去官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 使用xs ...

  3. ubuntu 14.04常见问题

    1. 改root密码 sudo passwd root 2. 显示登录框 sudo gedit /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf 添加 ...

  4. Exception (1) Understanding Exception Handling

    When an exception is thrown, it cannot be ignored--there must be some kind of notification or termin ...

  5. 【扫盲贴】为什么屏幕分辨率是 640x480

    本文原地址:http://www.easyx.cn/skills/View.aspx?id=172 常见的屏幕分辨率很奇怪,为什么总用一些不零不整的数字?比如以前最常见的分辨率是 640x480,当初 ...

  6. 什么是 Java 内存模型,最初它是怎样被破坏的?(转载)

    活跃了将近三年的 JSR 133,近期发布了关于如何修复 Java 内存模型(Java Memory Model, JMM)的公开建议.原始 JMM 中有几个严重缺陷,这导致了一些难度高得惊人的概念语 ...

  7. Middleware / 中间件

    中间件格式 module.exports = options => { return (ctx, next) => { // do something } } 中间件格式为一个高阶函数,外 ...

  8. 昨天发现的nginx默认不支持下划线引起angular-phonecat项目部分文件404问题

    今天发现原来确实是不存在这个文件... 因为我的Ubuntu的git没有安装,所以导致bower install时没有拉下来依赖包,因为bower是使用的git拉的包. 但是同时也搜到了nginx不支 ...

  9. Apache Shiro去掉URL中的JSESSIONID

    如果你的shiro版本在1.3.2版本以上这个BUG已经解决只需要在配置文件如下配置中添加红色部分 <!-- 会话管理器 --> <bean id="sessionMana ...

  10. AI_图像识别

    http://ai.baidu.com/forum/topic/show/595938 经测试识别率相当高,车辆信息识别 文中的import cv2不是python3自带库,需要安装pip3 inst ...