目标类
UserDaoImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.course.dao.impl;
public class UserDaoImpl {
public void addUser(String userName) { System.out.println("Save user, userName = " + userName); }
public void deleteUser(Integer userId) { System.out.println("delete user, userId = " + userId); int[] a = new int[10]; a[10] = 20; }
}
|
目标类
TimeConsumption.java
1 2 3 4 5 6 7 8 9 10 11 12
| package com.course.experiment;
public class TimeConsumption { public void circulation() { int sum = 0; for (int i = 0; i < 100; i++) { sum = sum + i; } System.out.println("sum = " + sum); } }
|
切面类
MyDMLAdvice.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.course.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyDMLAdvice {
public void openTransaction() { System.out.println("open transaction"); }
public void commitTransaction() { System.out.println("commit transaction"); }
public void dmlTransaction(ProceedingJoinPoint pj){ try { openTransaction(); pj.proceed(); commitTransaction(); } catch (Throwable e) { e.printStackTrace(); } }
public void afterThrowing(Throwable throwable) throws Exception { System.out.println("产生异常:" + throwable.getMessage()); } }
|
切面类
MyTimeConsumptionAdvice.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.course.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyTimeConsumptionAdvice {
public void computeTimeConsumption(ProceedingJoinPoint pj){ try { long startTime = System.currentTimeMillis(); pj.proceed(); long endTime = System.currentTimeMillis(); System.out.println("time consumption = " + (endTime - startTime) + " ms"); } catch (Throwable e) { e.printStackTrace(); } } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.dao.impl.UserDaoImpl; import com.course.experiment.TimeConsumption;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); UserDaoImpl userDaoImpl = (UserDaoImpl) applicationContext.getBean("myUserDaoImpl");
userDaoImpl.addUser("user1");
userDaoImpl.deleteUser(2);
TimeConsumption timeConsumption = applicationContext.getBean(TimeConsumption.class); timeConsumption.circulation(); } }
|
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="myUserDaoImpl" class="com.course.dao.impl.UserDaoImpl"></bean> <bean id="timeConsumption" class="com.course.experiment.TimeConsumption"></bean> <bean id="myAdvise" class="com.course.advice.MyDMLAdvice"></bean> <bean id="myTimeConsumptionAdvice" class="com.course.advice.MyTimeConsumptionAdvice"></bean> <aop:config> <aop:aspect ref="myAdvise"> <aop:pointcut expression="execution(* com.course.dao.impl.*.*(..))" id="myPointCut1"/> <aop:before method="openTransaction" pointcut-ref="myPointCut1"/> <aop:after method="commitTransaction" pointcut-ref="myPointCut1"/> <aop:around method="dmlTransaction" pointcut-ref="myPointCut1"/> <aop:after-throwing method="afterThrowing" throwing="throwable" pointcut-ref="myPointCut1"/> </aop:aspect> </aop:config> <aop:config> <aop:aspect ref="myTimeConsumptionAdvice"> <aop:pointcut expression="execution(* com.course.experiment.*.*(..))" id="myPointCut2"/> <aop:around method="computeTimeConsumption" pointcut-ref="myPointCut2"/> </aop:aspect> </aop:config> </beans>
|