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();
}
}
}
spring切面拦截实现的更多相关文章
- 从零开始学 Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
- Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
- Spring使用拦截器支持国际化(转)
Spring使用拦截器支持国际化很方便,使用时只需要两个步骤: 一.spring配置 具体配置方式如下: <!-- 资源文件绑定器,文件名称:messages.properties(没有找到时的 ...
- Spring 中拦截器与过滤器的区别
spring 中拦截器 与servlet 的filter 有相似之处.比如二者都是aop 编程思想的体现都能实现权限检查,日志记录等. 不同之处 使用范围不同 Filter 是Servlet 规定的. ...
- spring MVC拦截器01
spring MVC拦截 作用:身份校验,权限检查,防止非法訪问. 场景:一个bbs系统,用户没有登录就无法发帖或者删除评论; 一个博客系统,没有登录就无法发表博文,无法添加分类,无法删除博文. sp ...
- spring boot拦截器配置
1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...
- spring mvc拦截器原理分析
我的springMVC+mybatis中的interceptor使用@autowired注入DAO失败,导致报空指针错误,这个是为什么呢? :空指针说明没有注入进来,你可以检查一下你的这个拦截器int ...
- SSM(spring mvc+spring+mybatis)学习路径——2-2、spring MVC拦截器
目录 2-2 Spring MVC拦截器 第一章 概述 第二章 Spring mvc拦截器的实现 2-1 拦截器的工作原理 2-2 拦截器的实现 2-3 拦截器的方法介绍 2-4 多个拦截器应用 2- ...
- Spring 注解拦截器使用详解
Spring mvc拦截器 平时用到的拦截器通常都是xml的配置方式.今天就特地研究了一下注解方式的拦截器. 配置Spring环境这里就不做详细介绍.本文主要介绍在Spring下,基于注解方式的拦截器 ...
随机推荐
- cakephp中sql查询between
$trading_list = $this->Trading->find('all', array('conditions' => array('buy_time BETWEEN ? ...
- [Training Video - 3] [Groovy in Detail] Non-static and Static variables, objects and object referances
log.info "starting" // we use class to create objects of a class Planet p1 = new Planet() ...
- Linux Mint 17使用配置2
tee命令--作用:读取标准输入的数据, 并将其内容输出成文件 who | tee who.out # 将who命令的输出写入同目录文件who.out中 who | grep loger9567 | ...
- javascript总结19:javascript 使用概述
1 JS作用 1.验证表单(以前的网速慢)`` 2.页面特效(PC端的网页效果) 3.移动端(移动web和app) 4.异步和服务器交互(AJAX) 5.服务端开发(nodejs) 2 浏览器的主要构 ...
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
题意:给一个数 n,让你求一个排列,使得这个排列与0-n的对应数的异或之最大. 析:既然是异或就得考虑异或的用法,然后想怎么才是最大呢,如果两个数二进制数正好互补,不就最大了么,比如,一个数是100, ...
- C#结构(Struct)
Struct简介: 结构是使用 struct 关键字定义的,与类相似,都表示可以包含数据成员和函数成员的数据结构. 1.结构是一种值类型,并且不需要堆分配. 它都放在堆栈上2.结构的实例化可以不使用 ...
- Spring MVC @RequestMapping浅析
简介:@RequestMappingRequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.RequestMapp ...
- 编写高质量代码改善C#程序的157个建议——建议82:Parallel简化但不等同于Task默认行为
建议82:Parallel简化但不等同于Task默认行为 建议81说到了Parallel的使用方法,不知道大家是否注意到文中使用的字眼:在同步状态下简化了Task的使用.也就是说,在运行Paralle ...
- 基于JSP+Servlet开发手机销售购物商城系统(前台+后台)源码
基于JSP+Servlet开发手机销售购物商城系统(前台+后台) 开发环境: Windows操作系统 开发工具:Eclipse/MyEclipse+Jdk+Tomcat+MySQL数据库 运行效果图: ...
- Shapefile点图层转换为Shapefile线图层
在Oracle数据表转换为Shapefile(一)和Oracle数据表转换为Shapefile(二)两篇文章中,分别介绍了两种不同的根据Oracle数据表生成Shapefile点图层的方法.本文在此基 ...