<aop:aspectj-autoproxy/>
然后Advice所使用的Bean需要在配置文件定义,添加后如下:
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.apache.derby.jdbc.EmbeddedDriver</value> </property> <property name="url"> <value>jdbc:derby:f:/zwh/mydb2</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>root</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.DerbyDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="annotatedClasses"> <list> <value>zwh.ssh.maven.po.User</value> </list> </property> </bean> <context:component-scan base-package="zwh.ssh.maven"></context:component-scan> <bean class="zwh.ssh.maven.aop.ServiceAdvice"></bean> <aop:aspectj-autoproxy/> </beans>
ServiceAdvice.java内容如下:
@Aspect public class ServiceAdvice { static Logger log = Logger.getLogger(ServiceAdvice.class); @Before("execution(* zwh.ssh.maven.service.impl.*.*(..))") public void before(JoinPoint jp) { log.info("before:" + "在方法" + jp.getSignature().getName() + "之前执行"); log.info("before:" + "方法的参数" + Arrays.toString(jp.getArgs())); log.info("before:" + "方法的目标对象" + jp.getTarget()); } @AfterReturning(returning="rvt", pointcut="execution(* zwh.ssh.maven.service.impl.*.*(..))") public void afterReturning(JoinPoint jp, Object rvt) { log.info("afterReturning:" + jp.getSignature().getName() + "方法返回" + rvt); } @AfterThrowing(throwing="ex", pointcut="execution(* zwh.ssh.maven.service.impl.*.*(..))") public void afterThrowing(JoinPoint jp, Throwable ex) { log.info("afterThrowing:" + jp.getSignature().getName() + "抛出异常" + ex); } @After("execution(* zwh.ssh.maven.service.impl.*.*(..))") public void after() { log.info("after:" + "在方法之后执行"); } @Around("execution(* zwh.ssh.maven.service.impl.*.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { log.info("around:" + "在方法" + pjp.getSignature().getName() + "之前执行"); Object[] args = pjp.getArgs(); log.info("around:" + "方法的参数" + Arrays.toString(args)); Object rvt = pjp.proceed(args); log.info("around:" + "方法" + pjp.getSignature().getName() + "的返回值" + rvt); return rvt; } }