1. 程式人生 > >在Spring中Aspectj框架定義的通知類型

在Spring中Aspectj框架定義的通知類型

roc bsp cee framework gty ins 輸出 org arch

目標對象接口
1
public interface IUserService { 2 public void add(); 3 public void update(); 4 public void del(); 5 public void search(); 6 }

1.創建目標對象

 1 public class UserServiceImpl implements IUserService {
 2     @Override
 3     public void add() {
 4         System.out.println("add....");
5 } 6 @Override 7 public void update() { 8 System.out.println("update...."); 9 } 10 @Override 11 public void del() { 12 System.out.println("del...."); 13 } 14 @Override 15 public void search() { 16 System.out.println("search...."); 17 }
18 }

2.創建通知(增強 advice)

 1 //adcice增強類
 2 public class UserServiceHelper {
 3     //前置通知
 4     public void before(JoinPoint jp){
 5         System.out.println("攔截目標類"+jp.getSignature().getDeclaringTypeName());
 6         System.out.println("攔截目標方法名"+jp.getSignature().getName());
 7         System.out.println("前置通知");
8 9 } 10 public void afterreturning(JoinPoint jp,Object val){ 11 System.out.println("目標返回值"+val); 12 System.out.println("後置通知"); 13 } 14 public Object around(ProceedingJoinPoint pjp) throws Throwable{ 15 System.out.println("環繞前通知"); 16 Object value = pjp.proceed(); 17 System.out.println("環繞後通知"); 18 return value; 19 } 20 public void afterthrowing(JoinPoint jp,Throwable ex){ 21 //有異常時才輸出 22 System.out.println("異常拋出通知"+ex); 23 } 24 public void after(JoinPoint jp){ 25 System.out.println("攔截目標方法名"+jp.getSignature().getName()); 26 System.out.println("最終通知"); 27 } 28 }

3.1在Spring的applicationContext.xml配置:

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:context="http://www.springframework.org/schema/context"
4     xsi:schemaLocation="
5         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
7     <import resource="./aop4.xml" />
8 </beans> 

3.2在Spring的xmp配置apo1.xml

 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"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 9         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> 
10     <!-- target 目標對象 -->
11     <bean id="userService" class="cn.aspectjs.UserServiceImpl"/>
12     <!-- advice 通知-->
13     <bean id="userServiceAdvice" class="cn.aspectjs.UserServiceHelper"/>
14     <!-- config是用來聲明,內容表示不管有沒有接口,都用aspectj框架實現aop,aspect用來配置切面 -->
15     <aop:config proxy-target-class="true">
16         <aop:aspect ref="userServiceAdvice">
17             <aop:pointcut expression="execution(* *.add(..))" id="MypointCut"/>
18             <!-- method是增強類 -->
19             <aop:before method=""/>
20              <aop:before method="before" pointcut-ref="MypointCut"/>
21             <aop:after-returning method="afterreturning" pointcut-ref="MypointCut" returning="val"/>
22             <aop:around method="around" pointcut-ref="MypointCut" />
23             <aop:after-throwing method="afterthrowing" pointcut-ref="MypointCut" throwing="ex"/>
24             <aop:after method="after" pointcut-ref="MypointCut" /> 
25         </aop:aspect>
26     </aop:config>
27 </beans> 

4.測試類

 //Spring整合JUnit測試
@RunWith(SpringJUnit4ClassRunner.class)
//參數定義了要裝入的Spring配置文件
2 @ContextConfiguration(locations="classpath:applicationContext.xml") 3 public class AspectJTest{ 4 @Autowired 5 private IUserService userService; 6 @Test 7 public void test1(){ 8 //對應aop1.xml中的aspectj定義的通知. 9 userService.add(); 10 } 11 }

在Spring中Aspectj框架定義的通知類型