<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="userManager" class="com.jzg.spring.UserManagerImpl" /> <bean id="logHandler" class="com.jzg.spring.LogHandler" /> <aop:config> <!-- 先声明一个aspect,这里logHandler为<bean id="logHandler">所指向的java类 --> <!--securityHandler中有需要在被切入方法上执行的方法 --> <aop:aspect id="logAspect" ref="logHandler"> <!-- 这里是匹配任意返回值的无参add前缀 的方法 --> <!-- <aop:pointcut id="addAllMethod" expression="execution(* add*(..))"/> --> <!-- execution(* com.jzg.spring.*.*(..)), 这里是匹配spring包下的所有方法 --> <aop:pointcut id="AllMethod" expression="execution(* com.jzg.spring.*.*(..))" /> <!-- 按照AllMethod切入点的规定,在spring包下所有方法执行后都会执行一次 log() 方法 --> <aop:after method="log" pointcut-ref="AllMethod" /> </aop:aspect> </aop:config> </beans>
package com.jzg.spring; import org.aspectj.lang.JoinPoint; public class LogHandler { @SuppressWarnings("unused") private void log(JoinPoint jp) { Object o[] = jp.getArgs(); System.out.println(jp.getSignature()); System.out.println(o[0]); System.out.println("-------log()----------"); } }