Spring--AOP(面向切面)编程
AOP
切面就像一把菜刀,将Java处理业务流程进行分割,在分割处添加特定的业务处理。主要应用于声明事务、安全和缓存。在本文中,主要介绍两种切面的实现方法--Java配置和XML配置。
Java配置
- 创建Java类
创建一个Music的Java类,用于声明切点
@Component
public class Music {
public void perform(int num){
System.out.println("music");
}
}
- 创建切面
创建Aop Java类,并声明为切面。声明为切面使用注解@Aspect,同时,切面必须是一个Bean。同时,声明一个切点,避免创建通知的时候重复使用过长的表达式。
@Component
@Aspect
public class Aop {
@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.*(..))")
public void performer(){}
@Before("performer()")
public void beforee(){
System.out.println("before");
}
@After("performer()")
public void afterr(){
System.out.println("after");
}
@AfterReturning("performer()")
public void afterReturning(){
System.out.println("afterreturning");
}
@AfterThrowing("performer()")
public void throwingg(){
System.out.println("throwing");
}
- 创建Java配置类
创建JavaConfiguration 类,创建Bean工厂,在这里需要使用@EnableAspectAutoProxy注解启动Spring切面功能
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class JavaConfiguration {
}
- 创建测试类
创建Main测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JavaConfiguration.class)
public class Main {
@Autowired
private Music music;
@Test
public void test(){
music.perform(3);
}
}
- 为通知传递参数
希望将声明为切点方法中的参数传递到通知当中其,则需要在声明切点的使用指明args参数,在Java中使用&& 在xml中使用and,下面是创建为通知传递参数的切面:
/*@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(int)) && args(number)")
public void performer(int number){}
@Before("performer(number)")
public void beforee(int number){
System.out.println("before" + number);
}
@After("performer(number)")
public void afterr(int number){
System.out.println("after" + number+2);
}
@AfterReturning("performer(number)")
public void afterReturning(int number){
System.out.println("afterreturning" + number+1);
}
@AfterThrowing("performer(number)")
public void throwingg(int number){
System.out.println("throwing" + number +3);
}
- 另外,还可以使用@Around穿件环绕通知,被声明为环绕通知的方法需要包含参数ProceedingJoinPoint,通过ProceedingJoinPoint.proceed()来区分前置 通知和后置通知,通过try...catch来获取异常通知
@Component
@Aspect
public class AopSecond {
@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(..))")
public void performer(){}
@Around("performer()")
public void per(ProceedingJoinPoint jb){
try{
System.out.println("beforeer");
jb.proceed();
System.out.println("afterr");
}catch(Throwable eThrowable){
System.out.println("exception");
}
}
}
XML中配置切面
- 创建Java类
@Component
public class AopXml {
public void performA(){
System.out.println("performA");
}
public void performB(){
System.out.println("performB");
}
}
- 创建切面类
@Component
public class AopConfug {
public void beforee(){
System.out.println("before");
}
public void afterr(){
System.out.println("after");
}
public void aroundd(ProceedingJoinPoint pb) throws Throwable{
System.out.println("beforeeee");
pb.proceed();
System.out.println("afterrrr");
}
}
- 创建xml配置文件,其中需要添加
xmlns:context="http://www.springframework.org/schema/context"和http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd,来支持component-scan,同时,还需要配置<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>支持自动注入
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="com.tidas.spring.fourth.aopxml"/>
<!-- 无参数 -->
<aop:config>
<aop:aspect ref="aopConfug">
<aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(..))" id="aopxmll"/>
<!-- 执行AopXml中的任何方法,都会通知切面 -->
<aop:before pointcut-ref = "aopxmll" method="beforee"/>
<aop:after pointcut-ref = "aopxmll" method="afterr"/>
<aop:around pointcut-ref = "aopxmll" method="aroundd"/>
<!-- 对于环绕通知,其实在xml中声明和其它通知声明一样,没有参数,而参数还是在具体的方法中就好 -->
</aop:aspect>
</aop:config>
</beans>
- 为通知传递参数
创建带参数的通知
//有参数
/*public void beforee(int number){
System.out.println("number:" + number);
}*/
创建带参数的方法
//有参数
public void perform(int number){
System.out.println("perform2");
}
切面和切点的配置,在xml中使用and连接args参数
<aop:config>
<aop:aspect ref="aopConfug">
<aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(int)) and args(number)" id="aopxmll"/>
<aop:before pointcut-ref = "aopxmll" method="beforee"/>
</aop:aspect>
</aop:config>
通过切面为Java引入新的功能
Spring--AOP(面向切面)编程的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
- Spring AOP 面向切面编程入门
什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...
- 详细解读 Spring AOP 面向切面编程(一)
又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
随机推荐
- 关于Idea中右边的maven projects窗口找不到了如何调出来
关于Idea中右边的maven projects窗口找不到了如何调出来? 具体的idea版本我不太清楚,我用的是2016版,其他版本应该也是一样的. 首先idea自带了maven控件,不像Eclip ...
- 使用MVC创建API
1.新建MVC-WebAPI 2.Build后页面是这样的,这就是我们需要的页面. 3.自己Add API的页面,然后Run,会发现页面没有action和Description 4.显示action, ...
- iOS masonry九宫格 单行 多行布局
Masonry是个好东西,在当前尺寸各异的iOS开发适配中发挥着至关重要的作用,由于项目中Masonry布局用的比较多,对于UI布局也有了一些自己的理解,经常会有人问道Masonry布局九宫格要怎么布 ...
- IOS学习3——代理
本文转载自:你真的了解iOS代理设计模式吗? 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递一些参数.这篇文章会涵盖代理的使用技巧和原理,以及代理的内存 ...
- iOS OC Swift3.0 TableView 中tableviewcell的线左边不到边界
Swift 3.0 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt index ...
- bzoj 2109: [Noi2010]Plane 航空管制
Description 世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频 发生.最近,小X就因为航空管制,连续两次在机场被延误超过了两小时.对此, 小X表示很不满意. 在这次来烟台的 ...
- MySQL 字符集问题及安全的更新操作
一.字符集乱码 1.操作系统字符集 [root@mysql5 ~]# cat /etc/system-release /etc/sysconfig/i18n CentOS release 6.5 (F ...
- Java_Date_01_判断两个时间相差的天数
二.参考资料 1.java 判断两个时间相差的天数 2.java计算两个日期之间相差天数和相隔天数详解
- ajax调用数据案例,二级联动
题目:请针对移动端web浏览器制作一个结账数据信息展示页面 要求: 1. 页面样式除不使用表格呈现外,可自由选择其他呈现方式 2. 需符合移动端操作习惯 3. 可根据服务区.门店查询结账信息 4. 可 ...
- CSS图片翻转动画技术详解
因为不断有人问我,现在我补充一下:IE是支持这种技术的!尽管会很麻烦.需要做的是旋转front和back元素,而不是旋转整个容器元素.如果你使用的是最新版的IE,可以忽略这一节.IE10+是支持的,I ...