国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
一個(gè)簡(jiǎn)單的Spring的AOP例子 - dragon - BlogJava

      經(jīng)過(guò)這段日子的學(xué)習(xí)和使用Spring,慢慢地體會(huì)到Spring的優(yōu)妙之處,正在深入地吸收Spring的精華,呵呵。現(xiàn)在寫(xiě)的這個(gè)只是個(gè)簡(jiǎn)單AOP例子,包括前置通知,后置通知,環(huán)繞通知,和目標(biāo)對(duì)象。寫(xiě)這個(gè)例子的主要目標(biāo)只是想讓想學(xué)AOP的能更快地入門(mén),了解一下如何去配置AOP里面的東東。
目標(biāo)對(duì)象的接口:IStudent.java

 1
/**
 2
 * 
 3
 
*/

 4
package  com.dragon.study;
 5

 6
/**
 7
 * 
@author  dragon
 8
 *
 9
 
*/

10
public   interface  IStudent  {
11
    
12
    
public   void  addStudent(String name);
13
}

14


目標(biāo)類(lèi):StudentImpl.java

 1
/**
 2
 * 
 3
 
*/

 4
package  com.dragon.study.Impl;
 5

 6
import  com.dragon.study.IStudent;
 7

 8
/**
 9
 * 
@author  dragon
10
 *
11
 
*/

12
public   class  StudentImpl  implements  IStudent {
13

14
     
public   void  addStudent(String name) {
15
         System.out.println(
" 歡迎  " + name + "  你加入Spring家庭! " );
16
     }

17
}

18



前置通知:BeforeAdvice.java

 1
/**
 2
 * 
 3
 
*/

 4
package  com.dragon.Advice;
 5

 6
import  java.lang.reflect.Method;
 7

 8
import  org.springframework.aop.MethodBeforeAdvice;
 9

10
/**
11
 * 
@author  dragon
12
 *
13
 
*/

14
public   class  BeforeAdvice  implements  MethodBeforeAdvice {
15

16
      
public   void  before(Method method,Object[] args, Object target)
17
               
throws  Throwable {
18
          
19
          System.out.println(
" 這是BeforeAdvice類(lèi)的before方法. " );
20
          
21
      }

22
}

23

后置通知:AfterAdvice.java
 1
/**
 2
 * 
 3
 
*/

 4
package com.dragon.Advice;
 5

 6
import java.lang.reflect.Method;
 7

 8
import org.springframework.aop.AfterReturningAdvice;
 9

10
/**
11
 * 
@author dragon
12
 *
13
 
*/

14
public class AfterAdvice implements AfterReturningAdvice{
15
    
16
    
public void afterReturning(Object returnValue ,Method method,
17
                   Object[] args,Object target) 
throws Throwable{
18
        System.out.println(
"這是AfterAdvice類(lèi)的afterReturning方法.");
19
    }

20
      
21

22
}

23


環(huán)繞通知:CompareInterceptor.java

 1
/**
 2
 * 
 3
 
*/

 4
package com.dragon.Advice;
 5

 6
import org.aopalliance.intercept.MethodInterceptor;
 7
import org.aopalliance.intercept.MethodInvocation;
 8

 9

10
/**
11
 * 
@author dragon
12
 *
13
 
*/

14
public class CompareInterceptor implements MethodInterceptor{
15

16
      
public Object invoke(MethodInvocation invocation) throws Throwable{
17
          Object result 
= null;
18
         String stu_name 
= invocation.getArguments()[0].toString();
19
         
if ( stu_name.equals("dragon")){
20
             
//如果學(xué)生是dragon時(shí),執(zhí)行目標(biāo)方法,
21
              result= invocation.proceed();
22
              
23
         }
 else{
24
             System.out.println(
"此學(xué)生是"+stu_name+"而不是dragon,不批準(zhǔn)其加入.");
25
         }

26
        
27
          
return result;
28
      }

29
}

30

配置文件applicationContext.xml
 1
<?xml version="1.0" encoding="UTF-8"?>
 2
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 3

 4
<beans>
 5

 6
<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
 7
<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
 8
<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
 9
<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
10

11
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
12
  
<property name="proxyInterfaces">
13
    
<value>com.dragon.study.IStudent</value>
14
  
</property>
15
  
<property name="interceptorNames">
16
    
<list>
17
     
<value>beforeAdvice</value>
18
     
<value>afterAdvice</value>
19
    
<value>compareInterceptor</value>  
20
    
</list>
21
  
</property>
22
  
<property name="target">
23
    
<ref bean="studenttarget"/>
24
  
</property>
25

26
</bean>
27

28

29

30

31
</beans>


  現(xiàn)在開(kāi)始寫(xiě)測(cè)試類(lèi),Test.java
 1
/**
 2
 * 
 3
 
*/

 4
package com;
 5

 6
import org.springframework.context.ApplicationContext;
 7
import org.springframework.context.support.FileSystemXmlApplicationContext;
 8

 9
import com.dragon.study.IStudent;
10

11
/**
12
 * 
@author dragon
13
 *
14
 
*/

15
public class Test {
16

17
    
/**
18
     * 
@param args
19
     
*/

20
    
public static void main(String[] args) {
21
        
// TODO Auto-generated method stub
22
      ApplicationContext ctx = 
23
          
new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
24
      
25
      IStudent person 
= (IStudent)ctx.getBean("student");
26
      person.addStudent(
"dragon");
27
      
28
//      person.addStudent("javadragon");
29
    }

30

31
}

32
posted on 2006-12-03 03:29 javadragon 閱讀(754) 評(píng)論(8)  編輯  收藏

評(píng)論:
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-02-22 11:41 | freesky_zh
這個(gè)類(lèi)public class BeforeAdvice implements MethodBeforeAdvice
好像有點(diǎn)問(wèn)題,在Eclipse中會(huì)報(bào)錯(cuò)。  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-02-28 20:34 | javadragon
我又試了次,沒(méi)有錯(cuò)呀,如果你的還出現(xiàn)錯(cuò)誤,
請(qǐng)你把整個(gè)工程發(fā)給我試下
郵箱:newlong@126.com  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-03-10 10:54 | weichenggao
不錯(cuò),好例子,不過(guò)運(yùn)行該程序,還需要加入commons-logging.jar
請(qǐng)大家注意!  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-03-24 00:31 | 鳥(niǎo)不生蛋蛋的地方
Nice,u've done a good job, keep practice, keep thinking, then move forward. God's watching u ,ahahaha~  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-03-26 20:25 | javadragon
Thank you! if you don't guided i to how to program,my program capability can't improve. you give me a importnat thing--thinking.thanks again ! i will become stronger. Sorry for my english.haha  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-10-11 13:38 | pcz
AfterAdvice

不能在方法執(zhí)行后 在執(zhí)行啊!  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-10-12 22:06 | javadragon
有什么問(wèn)題?  回復(fù)  更多評(píng)論
  
# re: 一個(gè)簡(jiǎn)單的Spring的AOP例子 2007-11-12 18:16 | landon
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Spring AOP(創(chuàng)建切面理解)1
Spring AOP 常用的四種實(shí)現(xiàn)方式 - Spring之旅 - JavaEye技術(shù)社區(qū)
SpringFramework中的AOP簡(jiǎn)單使用
aspectJ
Spring AOP是什么?你都拿它做什么?
Spring AOP 學(xué)習(xí)小結(jié)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服