1.xml文件需要引入aop命名空间

2.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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <context:component-scan base-package="com.junge.demo"></context:component-scan> <aop:config>
<aop:aspect id="logAspect" ref="myLog">
<aop:pointcut expression="execution (* com.junge.demo.spring.dao..*(..))" id="point_cut"/>
<!-- <aop:before method="beforeFunc" pointcut-ref="point_cut"/>
<aop:after method="afterFunc" pointcut-ref="point_cut"/>
<aop:after-returning method="returnFunc" pointcut-ref="point_cut"/>
<aop:after-throwing method="throwExpFunc" pointcut-ref="point_cut" throwing="e"/> -->
<aop:around method="aroundFunc" pointcut-ref="point_cut"/>
</aop:aspect>
</aop:config>
</beans>

切点的配置:使用一个星号*是匹配所有(同级的);使用2个点..匹配任意(同级和任意子级),如:

execution (* com.junge.demo.spring.dao.*.*(..)) 匹配dao包下所有类的所有方法

execution (* com.junge.demo.spring.dao..*(..))  匹配dao包下所有类,包括子类的子类(以及子类的子类的子类等)的所有方法。

3.日志类(通知配置,可以获取被拦截方法的参数、返回结果、异常信息)

package com.junge.demo.spring.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; @Component("myLog")
public class MyLogImpl { public void beforeFunc() {
System.out.println("beforeFunc ..."); } public void afterFunc() {
System.out.println("afterFunc ..."); } public void returnFunc() {
System.out.println("returnFunc ..."); } public void throwExpFunc(Exception e) {
System.out.println("throwExpFunc ...");
System.out.println(e.getMessage()); } public void aroundFunc(ProceedingJoinPoint point) {
System.out.println("arount before invoke ..."); if (null != point.getArgs() && point.getArgs().length > 0) {
for (Object arg : point.getArgs()) {
System.out.println("args:" + JSONObject.toJSON(arg));
}
}
System.out.println(point.getTarget());
System.out.println(point.getThis());
System.out.println(point.getKind());
System.err.println(point.getClass());
System.out.println(point.getSignature()); try {
Object result = point.proceed();
System.out.println("result:" + JSONObject.toJSONString(result));
System.out.println("after return ...");
} catch (Throwable e) {
e.printStackTrace();
System.out.println("arount after throws ...");
} System.out.println("after ..."); } }

4.运行结果

5.spring和aop匹配的包maven配置如下,其中aspectjweaver和aspectjrt需要用1.8.11,用高版本的报错

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.junge.demo</groupId>
<artifactId>spring-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-demo</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.11</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
</dependency>
</dependencies>
</project>

Spring 使用xml配置aop的更多相关文章

  1. Spring、XML配置AOP

    新建一个AOP类: public class MyInterceptor2 { public void doAccessCheck(){ System.out.println("前置通知 & ...

  2. Spring基于XML配置AOP

    目录结构: D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\StudentService.java pac ...

  3. spring+mybaits xml配置解析----转

    一.项目中spring+mybaits xml配置解析 一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能 ...

  4. spring的xml配置声明以及相应的问题处理

    spring的xml配置声明:  xml配置声明 Code 问题处理 问题1 xml报错: cvc-elt.1: Cannot find the declaration of element 'bea ...

  5. spring中用xml配置构造注入的心得

    spring中用xml配置构造注入时,如果 <constructor-arg> 属性都是 ref ,则不用理会参数顺序 <constructor-arg ref="kill ...

  6. Spring 基于xml配置方式的AOP

    我们具体用代码来说明: 1.ArithmeticCalculator.java package com.proc; public interface ArithmeticCalculator { in ...

  7. Spring 基于xml配置方式的AOP(8)

    1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculator { 4 int ad ...

  8. Spring 基于XML配置

    基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...

  9. Spring基于注解配置AOP

    D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xml <?xml version="1.0" encoding ...

随机推荐

  1. cat查阅文件技巧

    一.打印除匹配行之外的其它行,使用-v 打印除$和#的注释行:cat file| grep -v ^$ | grep -v ^#

  2. 判断是否某App

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta name= ...

  3. JavaScript倒计时实现

    /** * 倒计时函数 * @param {String}} endTime 终止时间戳 */ const countDown = (endTime, callback) => { const ...

  4. 图集内子图压缩及 ETC2 fallback选项的作用

    今天研究发现,图集内的小图最好也是2的N次方或4的倍数 比如这个 采用ECT2 压缩后里面有些子图很花,就是压失败了 失败的原因是尺寸不合规则. 这个由16位改为32位就不花了,意思是当ECT2压缩失 ...

  5. Python模块定义和使用

    Python中所谓的模块就是一个Python文件,一个abc.py的文件就是一个名字叫abc的模块,一个xyz.py的文件就是一个名字叫xyz的模块.模块由代码.函数或类组成.编程中使用模块不仅可以提 ...

  6. ArcGIS自定义工具箱-显示地图文档结构

    ArcGIS自定义工具箱-显示地图文档结构 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:显示地图文档mxd的数据组织结构,数据框,图层,表 使用方法: 地图 ...

  7. Linux下,如何查看磁盘是否包含数据

    可以使用lquerypv -h来查看磁盘是否包含数据,或磁盘头是否被dd过.这在安装RAC的过程中,是非常实用的一个命令.如果不包括数据的话,那么如下所示: [ZFFR4CB2101:root]/]& ...

  8. lvs+nginx负载均衡

    1       学习目标 掌握什么是负载均衡及负载均衡的作用和意义. 了解lvs负载均衡的三种模式. 了解lvs-DR负载均衡部署方法. 掌握nginx实现负载均衡的方法. 掌握lvs+nginx负载 ...

  9. const命令声明变量应注意的几点

    对于复合类型的变量,变量名不指向数据,而是指向数据所在的地址.const命令只是保证变量名指向的地址不变,并不保证该地址的数据不变,所以将一个对象声明为常量必须非常小心. const person = ...

  10. $nextTick 的作用

    文档:深入响应式原理 Vue 实现响应式并不是数据发生变化之后 DOM 立即变化,而是按一定的策略进行 DOM 的更新. $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据 ...