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下,基于注解方式的拦截器 ...
随机推荐
- Spring.net 后处理器 可用来切换实例
.xml配置 <!--我们在Object.xml文件上将HexuObjectPostProcessor注册到上下文对象中去--> <object id="hexu" ...
- Spring.net 事件的注入
1.首先上客户端代码 static void Main(string[] args) { IApplicationContext ctx = ContextRegi ...
- 【转载】MySQL常用系统表大全
转载地址:http://blog.csdn.net/xlxxcc/article/details/51754524 MySQL5.7 默认的模式有:information_schema, 具有 61个 ...
- 版本控制-https svn服务器搭建和常用命令(centos 6.3)
Svn是比较优秀的版本控制工具,虽然功能和性能上无法和Git媲美,但由于其容易搭建和使用的特性,所以在各个小公司还是很受欢迎的.使用Git可参考<版本控制-Git服务器搭建和常用命令使用> ...
- Creating Custom UITableViewCells with NIB files
Maksim Pecherskiy 13 November 2012 Well this sucks. Apparently these days you can only use the Inter ...
- Sublime Text3下载激活汉化
一.下载 Sublime Text3下载 http://www.sublimetext.com/3 选择对应的版本下载即可. 二.激活 2.1 官网购买license key激活 2.2 网上查找各种 ...
- Linux RPM学习笔记
RPM(RedHat Package Manager) rp-pppoe-3.1-5.i386.rpm软件名称-版本号-编译次数-适合的硬件平台.扩展名 xxx-devel.rpm开发使用 xxx.n ...
- Partition--分区总结
1. 在SQL SERVER 2008 R2 SP2之前版本,对分区只支持到1000个分区,之后版本支持到15000个分区.2. 分区索引对齐并不要求索引和表使用同一分区方案,但要求两者使用的分区方案 ...
- 菜鸟的Xamarin.Forms前行之路——windows下VS运行ios模拟器调试
在Xamarin.Forms项目中,运行安卓模拟器是很方便的,但是想要运行IOS模拟器,相对而言是困难一点. 在参考一些资料后,发现很多是与Xamarin.studio有关的方法,尝试了许久没有成功. ...
- WPF成长之路------视频
今天偶然看到一篇博文,发现WPF原来还可以直接播放视频!于是在这里记录一下,以后方便使用: <MediaElement Source="C:\WINDOWS\system32\oobe ...