歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux網卡綁定 網卡bond

linux網卡綁定 網卡bond

日期:2017/3/3 13:08:37   编辑:Linux技術
最近用到了spring的aop,對aop(面向切面編程)記憶有了新的加深,鑒於方便別人也方便自己找資料,特地做了小demo來展示一下xml配置和注解實現的aop,展示了各種通知,並且在各種通知裡面獲得各種值,這裡要提一下,用xml配置做前置通知獲得參數的時候,出現過一個問題::0 formal unbound in pointcut ,這裡很搞了一會兒,網上也找了各種資料,實在感歎,盲目轉帖,各種不做自己判斷真是坑爹,基本十有八九都是同樣的答案,關鍵是爛答案,後來不斷嘗試,終於解決問題,下面會奉上解決方案
spring-aop-xml.xml配置
復制代碼
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <beans xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context-2.5.xsd 11 http://www.springframework.org/schema/aop
12 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 13
14
15
16 <!-- 要被攔截的類定義 -->
17 <bean id="aopService" class="com.pis.aop.AopService"/>
18 <!-- 被攔截類aopService的攔截類 -->
19 <bean id="serviceInterceptor" class="com.pis.aop.ServiceInterceptor"/>
20
21 <aop:config>
22 <!-- 聲明proxyService為一個切面 也就是攔截類-->
23
24 <aop:aspect id="asp" ref="serviceInterceptor">
25
26 <!-- pointcut切入點 即對目標的哪些方法進行攔截 expression是攔截表達式
27 最常用的是 execution(* com.pis.aop.*.*(..))
28 對所有方法進行攔截 第一個"*"號代表返回類型 第二個"*"代表com.pis.aop下面所有類
29 第三個"*"代表所有方法 "(..)代表所有參數"-->
30
31 <aop:pointcut id="target" expression="execution(String com.pis.aop.AopService.*(String)) "/>
32
33 <aop:pointcut id="target_1" expression="execution(* com.pis.aop.*.*(..))"/>
34
35
36
37 <!-- 設定前置通知 並且獲得參數值 實踐證明pointcut-ref注入target獲得參數是會報:0 formal unbound in pointcut的,而直接寫切入點表達式是可以的
38 在這裡糾結半天,事實也證明arg-names="str"是沒有用的 還是直接寫切入點並指定參數最有效 這裡的str要對應切面和被攔截類的方法的參數名
39 -->
40 <aop:before method="before" pointcut="execution(String com.pis.aop.AopService.*(String)) and args(str)" />
41
42
43
44 <!-- 設定後置通知 -->
45 <aop:after-returning method="afterReturn" pointcut-ref="target" returning="result"/>
46
47 <!-- 設定環繞通知 -->
48 <aop:around method="around" pointcut-ref="target"/>
49
50
51 <!-- 設定最終通知 -->
52 <aop:after method="after" pointcut-ref="target"/>
53
54
55 <!-- 設定異常通知 -->
56 <aop:after-throwing method="afterThrowing" pointcut-ref="target_1" throwing="e"/>
57
58
59 </aop:aspect>
60 </aop:config>
61
62 </beans>
復制代碼
AopService.java,即要被攔截的類
復制代碼
1 package com.pis.aop;
2
3 import org.springframework.stereotype.Component;
4
5
6 @Component("aopService")
7 public class AopService {
8
9
10
11 //用來驗證前置通知獲取參數
12 public String execute(String str){
13 System.out.println("終於輪到我execute方法執行了");
14 try {
15 throw new Exception("可愛的異常");
16 } catch (Exception e) {
17 // System.out.println(e.getMessage());
18 } finally {
19 // System.out.println("finally執行");
20 }
21 return "result";
22 }
23
24
25 public void excp() throws Exception{
26 throw new Exception("可愛的異常");
27 }
28
29 }
復制代碼
ServiceInterceptor.java 切面類 也就是攔截類(官方名詞:代理類)
復制代碼
1 package com.pis.aop;
2
3 import org.aspectj.lang.ProceedingJoinPoint;
4 import org.aspectj.lang.annotation.After;
5 import org.aspectj.lang.annotation.AfterReturning;
6 import org.aspectj.lang.annotation.AfterThrowing;
7 import org.aspectj.lang.annotation.Around;
8 import org.aspectj.lang.annotation.Aspect;
9 import org.aspectj.lang.annotation.Before;
10 import org.aspectj.lang.annotation.Pointcut;
11 import org.springframework.stereotype.Component;
12
13
14 //定義切面 也就是攔截類
15 @Component("serviceInterceptor")
16 @Aspect
17 public class ServiceInterceptor {
18
19 //定義一個切入點 攔截的是execute方法
20 @Pointcut("execution(String com.pis.aop.AopService.*(String))")
21 public void pointcut(){}
22
23 //定義前置通知
24 @Before("pointcut() && args(str)")
25 public void before(String str){
26 System.out.println("***進入前置通知,獲取參數:***");
27 }
28
29 //這裡的returning的值result必須和afterReturn的參數一致 pointcut引入寫法較其他方法有所不同
30 @AfterReturning(pointcut="pointcut()", returning="result")
31 public void afterReturn(String result){
32 System.out.println("***進入後置通知,獲取返回結果:"+result+"***");
33 }
34
35 //定義最終通知
36 @After("pointcut()")
37 public void after(){
38 System.out.println("***進入最終通知***");
39 }
40
41 //自定義了一個切入點,並獲得異常信息
42 @AfterThrowing(pointcut="execution(* com.pis.aop.*.*(..))", throwing = "e")
43 public void afterThrowing(Exception e){
44 System.out.println("***進入例外通知,獲得異常信息:"+e+"***");
45 }
46
47 //定義環繞通知
48 @Around("pointcut()")
49 public Object around(ProceedingJoinPoint pjp){
50 Object obj=null;
51 System.out.println("***進入環繞通知***");
52 try {
53 obj = pjp.proceed();//此處返回的是攔截的方法的返回值,如果不執行此方法,則不會執行被攔截的方法,利用環繞通知可以很好的做權限管理
54 } catch (Throwable e) {
55 e.printStackTrace();
56 }
57 System.out.println("***退出環繞通知***");
58 return obj;
59 }
60 }
復制代碼
AopTest.java 專門來測試aop的類,junit來寫的,直接改成main方法亦可以
復制代碼
1 package com.pis.aop;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8
9
10 public class AopTest {
11 ApplicationContext atx = null;
12 AopService aopService = null;
13 @Before
14 public void before(){
15 atx = new ClassPathXmlApplicationContext("/spring/spring-aop-xml.xml");
16 // atx = new ClassPathXmlApplicationContext("/spring/spring-aop-annotation.xml");
17 aopService = (AopService)atx.getBean("aopService");
18 }
19
20
21
22
23 //前置通知
24 @Test
25 public void testExecute(){
26 aopService.execute("param");
27 }
28
29 //例外通知
30 @Test
31 public void testExcp() throws Exception{
32 aopService.excp();
33 }
34
35
36 }
復制代碼
注解版:
spring-aop-annotation.xml 對,零配置 ,就是這麼簡單
復制代碼
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <beans xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans
8 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context-2.5.xsd 11 http://www.springframework.org/schema/aop
12 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 13
14
15 <aop:aspectj-autoproxy/> <!-- 開啟自動掃描注解 -->
16
17 <context:component-scan base-package="com.pis.aop"/> <!-- 指定在哪個包范圍掃描 -->
18
19
20
21 </beans>
復制代碼
被攔截類和切面與上面的一樣,我是xml配置測試完了之後,直接在原有代碼上面加的注解而已
AopTest.java 注解版的就是切換了一個xml文件而已,從spring-aop-xml.xml配置換成了spring-aop-annotation.xml配置
復制代碼
1 package com.pis.aop;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8
9
10 public class AopTest {
11 ApplicationContext atx = null;
12 AopService aopService = null;
13 @Before
14 public void before(){
15 // atx = new ClassPathXmlApplicationContext("/spring/spring-aop-xml.xml");
16 atx = new ClassPathXmlApplicationContext("/spring/spring-aop-annotation.xml");
17 aopService = (AopService)atx.getBean("aopService");
18 }
19
20
21
22
23 //前置通知
24 @Test
25 public void testExecute(){
26 aopService.execute("param");
27 }
28
29 //例外通知
30 @Test
31 public void testExcp() throws Exception{
32 aopService.excp();
33 }
34
35
36 }
復制代碼
ok!上面的兩種配置都已完成,測試不同版本的時候,只需切換讀取的配置文件即可,做就做全,這裡給出了前置通知獲取參數,後置通知獲取返回值,例外通知獲得異常的例子,特別是前置通知獲得參數鬧了好久,兩種版本的獲值均已實現,下面給出截圖,如有問題,歡迎拍磚,
這裡是執行testExecute的結果,從這裡可以很清晰的看到各個通知的執行順序 前置通知-環繞通知-方法-後置通知-環繞通知-最終通知(對應的是finally),例外通知只能是連接點出異常的時候才會有,所有我特地寫了兩個測試方法,testExcp就是專門測試例外通知的
testExecute結果圖
testExcp結果圖 可以看見異常被攔截了
最後要提下,環繞通知中可以做很多事,比如權限攔截,日志攔截等,可以看切面的攔截方法around就知道,另外附上自己對各個aop名稱的理解
aspect:切面 攔截類
joinpoint: 連接點 被攔截的方法
advice: 通知 攔截到方法之後要執行的各種操作,比如各種通知
cutpoint: 切入點 對哪些連接點進行的定義,也就是對那些被攔截方法或方法的集合的定義
Copyright © Linux教程網 All Rights Reserved