首页 技术 正文
技术 2022年11月6日
0 收藏 971 点赞 711 浏览 4053 个字

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.运行结果

Spring 使用xml配置aop

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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,905
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727