aop表达式写法

配置文件代码:

<?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/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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.swift"></context:component-scan> <bean id="book" class="com.swift.aop.Book"></bean>
<bean id="adviceBook" class="com.swift.aop.AdviceBook"></bean> <aop:config>
<!-- 切入点表达式第一个*表示public private等权限后接空格,第二个*表示任意方法(..)表示参数 -->
<aop:pointcut expression="execution(* com.swift.aop.Book.*())" id="pointcut1"/> <!-- 哪个切面(用来增强切入点的类) 名称是什么用ref表示 -->
<aop:aspect ref="adviceBook">
<!-- 增强的具体方法,增强哪个切入点 -->
<aop:before method="before" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>

包括bean context aop三个约束

以及切面的配置——表达式execution含义、advice通知/增强设置


连接点joinpoint的类,即需要被增强的类:

package com.swift.aop;

public class Book {
public String fun() {
System.out.println("This is Book's fun()..............");
return "This is Book's fun()..............";
}
}

进行切面操作的类:

package com.swift.aop;

public class AdviceBook {
public String before() {
System.out.println("This is AdviceBook's before()...............");
return "This is AdviceBook's before()...............";
}
}

测试的类:

package com.swift.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.swift.aop.Book;
@WebServlet("/test")
public class ServleTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServleTest() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
Book book=(Book)context.getBean("book");
response.getWriter().append(book.fun());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

演示效果图:

浏览器无反应

只有Book的方法,没有前置的before,想来应该是调用了但是返回的字符串没能显示在浏览器上

控制台console成功

前置增强在切入点前输出


如果是后置增强,只需要在增强的类中,增加后置增强的方法

public String after() {
System.out.println("This is AdviceBook's after()...............");
return "This is AdviceBook's after()...............";
}

然后在xml配置文件中添加切面配置的代码

<aop:after-returning method="after" pointcut-ref="pointcut1"/>

即可。


还有一个环绕的方法,有一个很长的表达不好记 ProceedingJoinPoint这个参数的对象有个方法,执行就是切入点的方法

在增强类中写方法

public String around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("This is AdviceBook's front()...............");
proceedingJoinPoint.proceed();
System.out.println("This is AdviceBook's end()...............");
return "This is AdviceBook's around()...............";
}

在<aop:config></aop:config>中写

<aop:around method="around" pointcut-ref="pointcut1"/>

===================================================================================================

重新整理上边内容

切入点

package cn.itcast.f_aspect;

public class CRUD {
public void add() {
System.out.println("增加内容");
int i=1/0;
}
public void delete() {
System.out.println("删除内容");
}
public void update() {
System.out.println("修改内容");
}
public void find() {
System.out.println("查找内容");
}
}

增强

package cn.itcast.f_aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class Myadvice {

    public void before() {
System.out.println("之前-->开启事务");
}
public void round(ProceedingJoinPoint proceedingJoinPoint) {
System.out.println("之前-->开启事务");
try {
proceedingJoinPoint.proceed();
System.out.println("没有异常,之后-->提交事务");
} catch (Throwable e) {
System.out.println("出现异常,之后--回滚事务");
}finally {
System.out.println("有无异常都执行,之后-->关闭事务");
} }
public void afterReturning() {
System.out.println("没有异常,之后-->提交事务");
}
public void after() {
System.out.println("有无异常都执行,之后-->关闭事务");
}
public void afterThrowing() {
System.out.println("出现异常,之后--回滚事务");
}
}

通过XML配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!-- 开启ioc注解 -->
<context:component-scan base-package="cn.itcast"></context:component-scan> <!---->
<bean name="car1" class="cn.itcast.domain.Car">
<property name="name" value="五菱宏光"></property>
</bean>
<bean name="car2" class="cn.itcast.domain.Car">
<property name="name" value="长安CS95"></property>
</bean>
<bean name="crud" class="cn.itcast.f_aspect.CRUD"></bean>
<bean name="myadvice" class="cn.itcast.f_aspect.Myadvice"></bean> <aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(public void cn.itcast.f_aspect.CRUD.*())" id="pointcut1"/>
<!-- 切面两部分:增强和切入点 -->
<aop:aspect ref="myadvice">
<aop:before method="before" pointcut-ref="pointcut1"/>
<aop:around method="round" pointcut-ref="pointcut1"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut1"/>
<aop:after method="after" pointcut-ref="pointcut1"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config> </beans>

测试类

package cn.itcast.f_aspect;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DemoCRUD { @Autowired
private CRUD crud;
@Test
public void execute() {
crud.add();
}
}

结果

Spring框架中的aop操作之二 通过配置文件实现增强的更多相关文章

  1. Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束

    (aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...

  2. Spring框架中的AOP技术----配置文件方式

    1.AOP概述 AOP技术即Aspect Oriented Programming的缩写,译为面向切面编程.AOP是OOP的一种延续,利用AOP技术可以对业务逻辑的各个部分进行隔离,从使得业务逻辑各部 ...

  3. spring框架中的aop技术

    1. 什么是AOP, 面向切面编程 AOP为Aspect Oriented Programming的缩写, 意为:面向切面编程,主要是使各部分之间的耦合度降低, 提高程序的可重用性, 同时提高了开发的 ...

  4. Spring框架中的AOP技术----注解方式

    利用AOP技术注解的方式对功能进行增强 CustomerDao接口 package com.alphajuns.demo1; public interface CustomerDao { public ...

  5. Spring Boot中使用AOP统一处理Web请求日志

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...

  6. Spring Boot中使用AOP记录请求日志

    这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...

  7. 设计模式(二十一)——解释器模式(Spring 框架中SpelExpressionParser源码分析)

    1 四则运算问题 通过解释器模式来实现四则运算,如计算 a+b-c 的值,具体要求 1) 先输入表达式的形式,比如 a+b+c-d+e,  要求表达式的字母不能重复 2) 在分别输入 a ,b, c, ...

  8. Spring框架IOC和AOP介绍

    说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...

  9. 转-Spring Framework中的AOP之around通知

    Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...

随机推荐

  1. 进击python第二篇:初识

    入门拾遗 模块 模块用以导入python增强其功能扩展 1.使用 import [模块名] 导入,应用方式:模块. 函数,例: >>> import math >>> ...

  2. CF438E The Child and Binary Tree(生成函数+多项式开根+多项式求逆)

    传送门 可以……这很多项式开根模板……而且也完全不知道大佬们怎么把这题的式子推出来的…… 首先,这题需要多项式开根和多项式求逆.多项式求逆看这里->这里,这里讲一讲多项式开根 多项式开方:已知多 ...

  3. IT兄弟连 JavaWeb教程 Servlet会话跟踪 Session常用方法

    ●  public Object getAttribute(String name) 该方法返回在该session会话中具有指定名称的对象,如果没有指定名称的对象,则返回null. ●  public ...

  4. GitHub使用方法(初级)

    [初识Github] Git 是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大成功,尤其是在Ruby社区中.目 ...

  5. bzoj2825:[AHOI2012]收集资源

    传送门 看到数据范围这么小,就没想过暴力的办法么 考虑肯定是从近走到远,所以走的点之间一定没有其他的点,所以我们就可以暴力的建图,然后暴力的去dfs就好了 代码: #include<cstdio ...

  6. GYM 101673E(暴搜预处理)

    1.不会超过500个不同的串-- 2.样例没给has到has是怎么样的,实测是true. 3.记忆化别剪错枝就好,嘤嘤嘤-- const int maxn = 505 + 5; int n, m, t ...

  7. Ubuntu下修改权限时出现"unable to execute /bin/chmod: Argument list too long"

    Ubuntu14.04下修改权限时出现"unable to execute /bin/chmod: Argument list too long"   ,大概意思是卧槽,你这参数也 ...

  8. C51存储的优化

    我们知道51单片机只有128BYTE的RAM, 有的增强型有XRAM,此时编程时就要注意,否则就有可能超出空间 1 对于各模块的全局变量.静态变量.全局枚举型常量,静态枚举型常量.个别操作存储(如外部 ...

  9. 1、二维数组中的查找------------>剑指offer系列

    题目 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...

  10. 2017 清北学堂 Day 6终极考试报告

    预计分数: 100+70+70 = 240 实际假分数 : 40+80+70= 190  in cena(好吧不得不承认这个分数,,,,,,=.=) 实际真分数 : 100+80+100 = 280 ...