1.准备好一个接口和实现类
接口
package com. lixv. service;
public interface UserService {
public void add ( int a) ;
public void delete ( ) ;
public void update ( ) ;
public void selete ( ) ;
}
实现类
package com. lixv. service;
public class UserServiceImpl implements UserService {
@Override
public void add ( int a) {
System. out. println ( "增加了一个用户" ) ;
}
@Override
public void delete ( ) {
System. out. println ( "删除了一个用户" ) ;
}
@Override
public void update ( ) {
System. out. println ( "更新了一个用户" ) ;
}
@Override
public void selete ( ) {
System. out. println ( "查询了一个用户" ) ;
}
}
2.创建前置日志
package com. lixv. log;
import org. springframework. aop. MethodBeforeAdvice;
import java. lang. reflect. Method;
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before ( Method method, Object[ ] args, Object target) throws Throwable {
System. out. println ( target. getClass ( ) . getName ( ) + "的" + method. getName ( ) + "被执行了" ) ;
}
}
前置日志实现spring的MethodBeforeAdvice 接口。 before方法会在目标方法执行之前执行。 before的method参数是目标方法的反射。 before的args参数是目标方法的参数。 before的target参数是目标对象。
3.创建后置日志
package com. lixv. log;
import org. springframework. aop. AfterReturningAdvice;
import java. lang. reflect. Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning ( Object returnValue, Method method, Object[ ] args, Object target) throws Throwable {
System. out. println ( "执行了" + method. getName ( ) + "方法,返回结果为:" + returnValue+ "参数是" + args[ 0 ] ) ;
}
}
后置日志实现spring的AfterReturningAdvice 接口。 afterReturning方法会在目标方法执行之后执行。 afterReturning的returnValue参数是方法的返回值。 afterReturning的method参数是目标方法的反射。 afterReturning的args参数是目标方法的参数。 afterReturning的target参数是目标对象。
4.在beans中配置aop
< 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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd" >
5.创建bean
< bean id = " userService" class = " com.lixv.service.UserServiceImpl" />
< bean id = " beforeLog" class = " com.lixv.log.BeforeLog" />
< bean id = " afterLog" class = " com.lixv.log.AfterLog" />
6.使用aop标签来配置前置日志和后置日志
< aop: config>
< aop: pointcut id = " pointcut" expression = " execution(* com.lixv.service.UserServiceImpl.*(..))" />
< aop: advisor advice-ref = " beforeLog" pointcut-ref = " pointcut" />
< aop: advisor advice-ref = " afterLog" pointcut-ref = " pointcut" />
</ aop: config>
aop:pointcut的expression属性是选择那些方法,上例是UserServiceImpl中的全部方法。 aop:advisor将前置日志或者后置日志配置进去。 advice-ref指向之前实现前置日志或后置日志接口的类。 pointcut-ref指向aop:pointcut的id。
7.测试
@Test
public void test2 ( ) {
ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext ( "beans.xml" ) ;
UserService userService = ( UserService) classPathXmlApplicationContext. getBean ( "userService" ) ;
userService. add ( 5 ) ;
}
8.运行结果