spring框架应用系列三:切面编程(带参数)
切面编程(带参数)
本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7786715.html
解决问题
1、分离业务监控与业务处理。简单点说,让开发人员更专注业务逻辑开发,类似于打印日志、统计时间、监控等等独立成一个单独的类,在需要的时候,动态的将代码切入到类的指定方法上,使方法拥有更强大的功能;
2、解决代码重复性,降低代码复杂程度;
内容说明
1、通过@Component注解,扫描(Magician)bean并注册到spring容器中时,需在XML配置文件中引入 <context:component-scan base-package="com.spring.example.aspectArgs"/>;
2、明确切面、切点、通知概念,这里切面是Magician,切点是Volunteer的thinkOfSomething方法,通知是Magician中所包含方法,由xml或注解配置,下面会分别给出示例;
3、通过执行Volunteer的thinkOfSomething方法,从而执行Magician中相应的方法,达到通知的效果;
应用实例:截听志愿者内心真实想法
先列出相关接口以及类代码
切面实现接口MindReader(读心者)
package com.spring.example.aspectAspectJArgs; //读心者
public interface MindReader {
void interceptThoughts(String thoughts);
void getConclusion(String thoughts);
}
切点类志愿者实现接口Thinker
package com.spring.example.aspectAspectJArgs; /**
* 读心者赋予一个他需要截听内心感应的志愿者
*/
public interface Thinker {
void thinkOfSomething(String thoughts);
}
志愿者实体类Volunteer
package com.spring.example.aspectAspectJArgs; import org.springframework.stereotype.Component; /**
* Created by weixw on 2017/10/24.
*/
@Component
public class Volunteer implements Thinker { private String thoughts;
@Override
public void thinkOfSomething(String thoughts) {
this.thoughts = thoughts; } }
以下有两种方式实现此实例的切面编程:一、通过XML配置文件,二、AspectJ注解方式
其他代码都相同,不同处在spring 的xml配置文件以及切面类文件
一、通过XML配置文件:
切面(读心者)实体类
package com.spring.example.aspectArgs; /**
* Created by weixw on 2017/10/24.
*/ import org.springframework.stereotype.Component; /**
* 截听志愿者的内心感应和显示他们在想什么
*/
@Component
public class Magician implements MindReader { @Override
public void interceptThoughts(String thoughts) {
System.out.println("Intercepting volunteer's thoughts :" +thoughts);
} @Override
public void getConclusion(String thoughts) {
System.out.println("For your thoughts :" +thoughts);
System.out.println("I think you are right.");
} }
spring 配置文件 aspect-args.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.spring.example.aspectArgs"/> <aop:config>
<!--通过component-scan自动扫描,@Component注解将Magician注册到spring容器-->
<aop:aspect ref="magician">
<aop:pointcut id="thinking" expression="execution(* com.spring.example.aspectArgs.Thinker.thinkOfSomething(String))
and args(thoughts)"/>
<aop:before pointcut-ref="thinking" method="interceptThoughts" arg-names="thoughts"/>
<aop:after pointcut-ref="thinking" method="getConclusion" arg-names="thoughts"/>
</aop:aspect>
</aop:config>
</beans>
二 、AspectJ注解方式
切面(读心者)实体类
package com.spring.example.aspectAspectJArgs; /**
* Created by weixw on 2017/10/24.
*/ import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 截听志愿者的内心感应和显示他们在想什么
*/
@Component
@Aspect
public class Magician implements MindReader { @Pointcut("execution(* com.spring.example.aspectAspectJArgs.Thinker.thinkOfSomething(String)) " +
"&& args(thoughts)") //定义切点
public void thinkOfSomething(String thoughts){}
@Before("thinkOfSomething(thoughts)")
@Override
public void interceptThoughts(String thoughts) {
System.out.println("Intercepting volunteer's thoughts :" +thoughts);
}
@AfterReturning("thinkOfSomething(thoughts)")
@Override
public void getConclusion(String thoughts) {
System.out.println("For your thoughts :" +thoughts);
System.out.println("I think you are right.");
} }
spring 配置文件 aspect-aspectJArgs.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.spring.example.aspectAspectJArgs"/>
<aop:aspectj-autoproxy/> </beans>
测试代码
注解测试代码如下,配置文件测试代码只需将配置文件名称改成spring/aspect-args.xml即可
package com.spring.example.aspectAspectJArgs;/**
* Created by weixw on 2017/10/19.
*/ import javafx.application.Application;
import javafx.stage.Stage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Driver extends Application { public static void main(String[] args) {
launch(args);
} @Override
public void start(Stage primaryStage) {
try { ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/aspect-aspectJArgs.xml");
Thinker thinker = (Thinker) ctx.getBean("volunteer");
thinker.thinkOfSomething("I'm stupid."); }catch (Exception e){
e.printStackTrace();
}
}
}
运行结果

总结
上述列出配置文件和注解两种方式都能实现切面编程。但对于自身业务开发,个人觉得注解方式较好,因为在修改某一切面类时不需要多处修改;
本文描述可能有不对或不全之处,欢迎大家吐槽!
不要让懒惰占据你的大脑,不要让妥协拖垮你的人生。青春就是一张票,能不能赶上时代的快车,你的步伐掌握在你的脚下。
spring框架应用系列三:切面编程(带参数)的更多相关文章
- Spring框架的第三天
## Spring框架的第三天 ## ---------- **课程回顾:Spring框架第二天** 1. IOC的注解方式 * @Value * @Resource(name="" ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
- spring框架应用系列四:切面编程(环绕通知与前后置通知区别)
切面编程(环绕通知与前后置通知区别) 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7867034.html 解决问 ...
- Spring实战4:面向切面编程
主要内容 面向切面编程的基本知识 为POJO创建切面 使用@AspectJ注解 为AspectJ的aspects注入依赖关系 在南方没有暖气的冬天,太冷了,非常想念北方有暖气的冬天.为了取暖,很多朋友 ...
- spring学习总结二-----面向切面编程(AOP)思想
上一篇spring博客简总结了spring控制反转和依赖注入的相关思想知识点,这篇博文对spring的面向切的编程思想进行简单的梳理和总结. 一.面向切面的思想 与面向对象的纵向关系概念不同,面向切面 ...
- 02 浅析Spring的AOP(面向切面编程)
1.关于AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- 10 Spring框架 AOP (三) Spring对AspectJ的整合
上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...
- Spring Boot2(六):使用Spring Boot整合AOP面向切面编程
一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop aop全称Aspec ...
- Spring(3):AOP面向切面编程
一,AOP介绍 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开 ...
随机推荐
- ②jquery复习
# jQuery 复习--by 传智前端与移动开发学院 ## 1. jQuery是什么?(了解)+ www.github.com+ jQuery 其实就是一堆的js函数,是普通的js,只不过应用广泛, ...
- E - 钱币兑换问题
在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input每行只有一个正整数N,N小于32768. Output对应每个输入,输出兑换方 ...
- ThinkPHP中:用户登录权限验证类
使用CommonAction.class.php公共类,统一判断用户是否登录 <?php //后台登录页 Class CommonAction extends Action{ //后台登录页面 ...
- Coin Change (II)(完全背包)
Coin Change (II) Time Limit: 1000MS Mem ...
- 从头编写 asp.net core 2.0 web api 基础框架 (2)
上一篇是: http://www.cnblogs.com/cgzl/p/7637250.html Github源码地址是: https://github.com/solenovex/Building- ...
- 【笔记】Kali linux的安装 和 一些使用前的准备工作(原创+转载)
该博文只记录笔者的蛇皮使用经历,纯新手= =,可能借鉴意义也可能没有(T _ T),侵删. 目录 kali linux 在个人计算机和在VirtualBox下的安装 kali linux 使用前准备工 ...
- 网时|ipone8爆冷,我的服务器空欢喜一场
上周ipone可谓是占尽了风头,从face ID到全面屏,从人脸识别到逆天价格,小编都能预想到上市之后的一片火热了,苹果后台的服务器恐怕都早已做好了准备,加大带宽,稳定运行,确保万无一失. 9月22日 ...
- jvm内存模型的理解
今天周六,又开始啃一遍<深入理解java虚拟机>每次读的感觉不一样,大学代码量较少,读起来也就死记硬背. 1.堆:长度可变,运行时使用的变量:存放对象(new )和数组之类: 2.栈:长度 ...
- try catch finally 中包含return的几种情况,及返回结果
当当当,兴致勃勃的第二篇博客,散花~ 下面是正题(敲黑板) 第一种情况:在try和catch中有return,finally中没有return,且finally中没有对try或catch中要 retu ...
- DNS—正、反向解析;委派;主从;子域;转发;智能dns等的实现
前言:DNS,耳熟能详的东西,内容太多,小编也不太好讲清,只能写几个实验详解,供大家参考. 一.简单介绍 1.DNS:通过主机名,最终得到该主机名对应的IP地址的过程叫做域名解析(或主机名解析). 端 ...