Spring 使用xml配置aop
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的更多相关文章
- Spring、XML配置AOP
新建一个AOP类: public class MyInterceptor2 { public void doAccessCheck(){ System.out.println("前置通知 & ...
- Spring基于XML配置AOP
目录结构: D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\StudentService.java pac ...
- spring+mybaits xml配置解析----转
一.项目中spring+mybaits xml配置解析 一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能 ...
- spring的xml配置声明以及相应的问题处理
spring的xml配置声明: xml配置声明 Code 问题处理 问题1 xml报错: cvc-elt.1: Cannot find the declaration of element 'bea ...
- spring中用xml配置构造注入的心得
spring中用xml配置构造注入时,如果 <constructor-arg> 属性都是 ref ,则不用理会参数顺序 <constructor-arg ref="kill ...
- Spring 基于xml配置方式的AOP
我们具体用代码来说明: 1.ArithmeticCalculator.java package com.proc; public interface ArithmeticCalculator { in ...
- Spring 基于xml配置方式的AOP(8)
1.ArithmeticCalculator.java 1 package com.proc; 2 3 public interface ArithmeticCalculator { 4 int ad ...
- Spring 基于XML配置
基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
- Spring基于注解配置AOP
D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xml <?xml version="1.0" encoding ...
随机推荐
- Linux权限管理之ACL权限
注:转载自:https://www.cnblogs.com/ysocean/p/7801329.html 目录 1.什么是 ACL 权限? 2.查看分区 ACL 权限是否开启:dump2fs ①.查看 ...
- 发送Http
/** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1& ...
- viewDidLoad, viewWillDisappear, viewWillAppear等区别及各自的加载顺序
ios 开发中视图的声明周期 viewWillAppear: 视图即将可见时调用.默认情况下不执行任何操作 viewDidAppear: 视图已完全过渡到屏幕上时调用 viewWillDisappea ...
- 数据库设计,表与表的关系,一对一。One-To-One(1)
如何处理对象间one-to-ont的映射关系:one-to-one: 处理一对一关联的方式有两种: 1.主键关联使用主键关联处理一对一的关系. 主键关联不需要额外的表字段:两行是通过这种一对一关系相关 ...
- elasticsearch-权威指南笔记-基础部分
参考这里的文档es权威指南 话说这个坑爹的文档是2.x版本的es,英文版本也是,所以就没啥好抱怨的了. 官方教程中有很多坑 例如,需要启动text上的索引. 还有就是get这个是不能带json的,所以 ...
- 根据文件夹更改样本文件名小程序.py
#按照文件名设置标签并将其插入文件名中import osimport shutil#获取目标文件夹的路径a= r'C:\Users\yy\Desktop\tianchi大赛\guangdong_rou ...
- jQuery开发工具
开发工具:MyEclipse2014 + aptana插件 下载apada 放到MyEclipse的路径 https://segmentfault.com/a/1190000005711923 ...
- python爬虫小说代码,可用的
python爬虫小说代码,可用的,以笔趣阁为例子,python3.6以上,可用 作者的QQ:342290433,汉唐自远工程师 import requests import refrom lxml i ...
- KALI安装与环境配置
2018-2019 201899224<网络攻防实践>第二周作业 虚拟化网络攻防实验环境包括以下部分: 靶机:包含系统和应用程序安全漏洞,并作为攻击目标的主机.(Windows XP和Li ...
- CSS如何作小于1PX的边
小于1PX的边能使页面变得更加精致,那么具体怎么做呢? 主要思路就是设置伪元素先放大再通过变换缩小. 代码如下 .border { position: relative;//如果有圆角 } .bord ...