環繞增強的功能比較強大
環繞增強的特點:
目標方法前后都可織入增強處理
功能最強大的增強處理
可獲取或修改目標方法的參數、返回值,可對他進行異常處理,甚至可以決定目標方法是否執行。
<aop:around> 定義壞繞增強
還是接着上一個案例的代碼進行改造
UserServiceLogger.java
1 //環繞增強 2 public void aroundLogger(ProceedingJoinPoint joinPoint) { 3 //下面是目標方法的前面執行的處理 4 logger.info("調用" + joinPoint.getTarget() + "的" 5 + joinPoint.getSignature() + "方法,方法參數是:" 6 + Arrays.toString(joinPoint.getArgs())); 7 Object result;//這個相當於是目標方法 8 try { 9 //下面是目標方法之后進行的處理 10 result = joinPoint.proceed(); 11 logger.info("調用"+joinPoint.getTarget()+"的"+joinPoint.getSignature()+"方法,方法返回值:"+result); 12 13 } catch (Throwable e) { 14 logger.error(joinPoint.getSignature().getName()+"方法發生異常"+e); 15 e.printStackTrace(); 16 } finally{ 17 18 } 19 }
UserDaoImpl.java
1 package dao.impl; 2 3 import dao.UserDao; 4 import entity.User; 5 6 /** 7 * 用戶DAO類,實現IDao接口,負責User類的持久化操作 8 */ 9 public class UserDaoImpl implements UserDao { 10 11 public void save(User user) { 12 // 這里並未實現完整的數據庫操作,僅為說明問題 13 System.out.println("保存用戶信息到數據庫"); 14 //throw new RuntimeException("為了測試程序異常"); 15 } 16 }
UserServiceImpl.java
1 package service.impl; 2 3 import service.UserService; 4 import dao.UserDao; 5 import entity.User; 6 7 /** 8 * 用戶業務類,實現對User功能的業務管理 9 */ 10 public class UserServiceImpl implements UserService { 11 12 // 聲明接口類型的引用,和具體實現類解耦合 13 private UserDao dao; 14 15 16 17 // 生成無參構造方法 18 public UserServiceImpl() { 19 20 } 21 22 // 帶參數構造方法 為dao進行賦值 23 public UserServiceImpl(UserDao dao) { 24 this.dao = dao; 25 } 26 27 28 public UserDao getDao() { 29 return dao; 30 } 31 32 // dao 屬性的setter訪問器,會被Spring調用,實現設值注入 33 public void setDao(UserDao dao) { 34 this.dao = dao; 35 } 36 37 public void addNewUser(User user) { 38 // 調用用戶DAO的方法保存用戶信息 39 dao.save(user); 40 System.out.println("注入進去的user對象的信息是:"+user.toString()); 41 } 42 }
在核心配置文件中配置環繞增強
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/aop 7 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 8 <!--以上是Spring框架的頭信息 使用p命名空間注入 --> 9 <bean id="dao" class="dao.impl.UserDaoImpl"></bean> 10 <bean id="service" class="service.impl.UserServiceImpl"> 11 <property name="dao" ref="dao"></property> 12 </bean> 13 <!-- 聲明增強方法所在的Bean --> 14 <bean id="theLogger" class="aop.UserServiceLogger"></bean> 15 <aop:config> 16 <!--定義切入點 --> 17 <aop:pointcut expression="execution(public void addNewUser(entity.User))" 18 id="pointcut" /> 19 <aop:aspect ref="theLogger"> 20 <aop:around method="aroundLogger" 21 pointcut-ref="pointcut" /> 22 </aop:aspect> 23 </aop:config> 24 </beans>
編寫測試方法
1 public class AopTest { 2 3 @Test 4 public void aopTest() { 5 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); 6 UserService a = (UserService) ctx.getBean("service"); 7 User user=new User(); 8 user.setUsername("丫丫"); 9 a.addNewUser(user); 10 } 11 12 }
運行結果:
12-30 18:14:52[INFO]aop.UserServiceLogger
-調用service.impl.UserServiceImpl@460d0a57的void service.UserService.addNewUser(User)方法,方法參數是:[entity.User@36c88a32]
保存用戶信息到數據庫
注入進去的user對象的信息是:entity.User@36c88a32
12-30 18:14:52[INFO]aop.UserServiceLogger
-調用service.impl.UserServiceImpl@460d0a57的void service.UserService.addNewUser(User)方法,方法返回值:null