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的延续,是软件开 ...
 
随机推荐
- STS安装
			
在eclipse中安装spring tool Suite插件需要根据eclipse版本找到对应的spring tool Suite安装包. spring tool Suite 官网地址:http:// ...
 - 网页端HTML使用MQTTJs订阅RabbitMQ数据
			
最近在做一个公司的日志组件时有一个问题难住了我.今天问题终于解决了.由于在解决问题中,在网上也查了很多资料都没有一个完整的实例可以参考.所以本着无私分享的目的记录一下完整的解决过程和实例. 需求:做一 ...
 - [js学习笔记] 原型链理解
			
js中只有对象,包括对象,函数,常量等. prototype 只有函数里有这个属性. 对象里可以设置这个属性,但是没这个属性的意义 prototype指向一个对象,可以添加想要的方法. 该对象里有一个 ...
 - 利用Docker快速创建Nginx负载均衡节点
			
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.Self-Host Kestrel 1. 在vs2017中新建dotnet core2. ...
 - Zend Framework 3.0 安装及创建初始化项目教程
			
前言: 最近开始接触关于PHP的框架的学习,然而PHP的框架少说也有七八种. 百度了一下,有人说ThinkPHP简单暴力的,有人说Laravel高大上的,等等等等,难以抉择. 最终我还是选择先从接触Z ...
 - 动易CMS - 设为首页代码和加入收藏代码(兼容各种浏览器)
			
注意: 这里虽然说是兼容,但是有些浏览器的设置就是不支持用js来把页面设为首页,加入收藏夹,只能让用户手动去在浏览器或者按键去设置这些功能,这里说的兼容是指当浏览器有这个设置的时候js会有提示. ...
 - Excel的实用函数
			
在介绍Excel函数前先说明两个概念:公式和函数. 公式:由用户自行设计对工作表进行计算和处理的计算式,以等号"="开始,其内部可以包括函数.引用.运算符和常量. 函数:即是预先定 ...
 - hdu3729二分匹配
			
I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
 - Hadoop(四)HDFS集群详解
			
前言 前面几篇简单介绍了什么是大数据和Hadoop,也说了怎么搭建最简单的伪分布式和全分布式的hadoop集群.接下来这篇我详细的分享一下HDFS. HDFS前言: 设计思想:(分而治之)将大文件.大 ...
 - 关于Android SDK Manager无法获取更新列表的正确设置
			
1.以"管理员身份运行"SDK Manager. 2.Android SDK Manager"=>"Tools"=>"Optio ...