Log.java:
package log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
/**
* Description: 使用spring实现aop---使用spring API来实现 前置通知
*/
public class Log implements MethodBeforeAdvice { //注意实现的这个接口,如果这个方法报错,那么导入aopalliance.jar和aspectweaver.jar两个jar包
/**
*
* @param method 被调用方法的对象,切入点的方法对象
* @param args 被调用的方法的参数
* @param target 被调用法方法的目标对象
* @throws Throwable
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
AfterLog.class:
package log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
/**
* Description:后置通知
*/
public class AfterLog implements AfterReturningAdvice { //注意实现的这个接口,如果这个方法报错,那么导入aopalliance.jar和aspectweaver.jar两个jar包
/**
* 目标方法执行后执行的通知
*
* @param returnValue 返回值
* @param method 被调用的方法
* @param args 被调用方法对象的参数
* @param target 被调用方法对象的目标对象
* @throws Throwable
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.print(target.getClass().getName() + "的" + method.getName() + "被成功执行,返回值是:" + returnValue);
}
}
UserService.class:
package service;
public interface UserService {
public void add();
public void delete();
public void search();
public void update();
}
UserServiceImp.class:
package service;
public class UserServiceImp implements UserService {
public void add() {
System.out.println("增加用户");
}
public void delete() {
System.out.println("删除用户");
}
public void search() {
System.out.println("查找用户");
}
public void update() {
System.out.println("修改用户");
}
}
applicationContext.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"
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">
<bean id="userService" class="service.UserServiceImp"/>
<bean id="log" class="log.Log"/>
<bean id="afterLog" class="log.AfterLog"/>
<aop:config>
<!--切入点 *:表示所有返回值-->
<!--()代表无参 (..)代表所有参数-->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImp.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
Test.java:
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
/**
* Description:
*/
public class Test {
public static void main(String args[]){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.add();
}
}
测试结果:
service.UserServiceImp的add方法被执行了
增加用户
service.UserServiceImp的add被成功执行,返回值是:null
Process finished with exit code 0
可以加我微信一起学习: