<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    雪山飛鵠

    溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.tkk7.com/sxyx2008/謝謝合作!!!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
    環境

    UserDAO
    package com.spring.dao;

    import org.springframework.stereotype.Component;

    @Component(
    "userDAO")
    public class UserDao {

        
    public void say() {
            System.out.println(
    "say method is called");
        }

        
    public void smile() {
            System.out.println(
    "smile method is called");
        }
        
        
    public void cry() {
            System.out.println(
    "cry method is called");
        }
        
        
    public void jump() {
            System.out.println(
    "jump method is called");
        }
    }
    不做過多解釋,不清楚的可參考上篇Spring AOP之HelloWorld xml配置
    UserService
    package com.spring.service;

    import javax.annotation.Resource;

    import org.springframework.stereotype.Component;

    import com.spring.dao.UserDao;

    @Component(
    "userService")
    public class UserService {
        
        @Resource(name
    ="userDAO")
        
    private UserDao dao;

        
    public UserDao getDao() {
            
    return dao;
        }

        
    public void setDao(UserDao dao) {
            
    this.dao = dao;
        }
        
        
    public void say() {
            dao.say();
        }

        
    public void smile() {
            dao.smile();
        }
        
        
    public void cry() {
            dao.cry();
        }
        
        
    public void jump() {
            dao.jump();
        }
    }
    Aop攔截類
    package com.spring.aop;

    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;

    @Component(
    "logIntercepter")
    @Aspect
    public class LogIntercepter {
        
        
    /*public void before(){
            System.out.println("----------before-------------");
        }
        
        public void after(){
            System.out.println("----------after-------------");
        }
        
        public void exception(){
            System.out.println("----------exception-------------");
        }
        
        public void around(){
            System.out.println("----------exception-------------");
        }
    */
        
        @Before(
    "execution(* com.spring.service..*.*(..))")
        
    public void before(){
            System.out.println(
    "----------before-------------");
        }
        
        @After(
    "execution(* com.spring.service..*.*(..))")
        
    public void after(){
            System.out.println(
    "----------after-------------");
        }
        
        @AfterThrowing(
    "execution(* com.spring.service..*.*(..))")
        
    public void exception(){
            System.out.println(
    "----------exception-------------");
        }
        
        
    /*@Around("execution(* com.spring.service..*.*(..))")
        public void around(){
            System.out.println("----------exception-------------");
        }
    */
        

        
        
    }
    注意觀察
    此類使用了@Aspect注解,你需要在spring配置文件中使用<aop:aspectj-autoproxy/>標簽開啟注解功能
    接下來依次定義了一系列方法before、after、exception、around依次標注注解為@Before("execution(* com.spring.service..*.*(..))") 、@After("execution(* com.spring.service..*.*(..))")、@AfterThrowing("execution(* com.spring.service..*.*(..))")、@Around("execution(* com.spring.service..*.*(..))") ,分別為spring aop 的前置通知、后置通知、異常通知、環繞通知,當進入com.spring.service包或子包下的所有方法時他們都會起作用,其中異常通知,只有在該方法出現異常時才會調用
    applicationContext.xml
    <?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:p
    ="http://www.springframework.org/schema/p"
        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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    >
        
    <context:annotation-config/>
        
    <context:component-scan base-package="com.spring.*"/>
        
    <aop:aspectj-autoproxy/>
        
        
    <!-- 
            <aop:config>
                <aop:aspect id="aspect" ref="logIntercepter">
                    <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/>
                    <aop:before method="before" pointcut-ref="pointCut"/>
                    <aop:after method="after" pointcut-ref="pointCut"/>
                    <aop:after-throwing method="exception" pointcut-ref="pointCut"/>
                        <aop:around method="around" pointcut-ref="pointCut"/>
                </aop:aspect>
            </aop:config>
        
    -->
            
    </beans>
    內容很簡單
    就這三行
    <context:annotation-config/>
    <context:component-scan base-package="com.spring.*"/>
    <aop:aspectj-autoproxy/>
    其中注視部分為xml配置部分的代碼
    單元測試
    package com.spring.test;

    import javax.annotation.Resource;

    import org.junit.Test;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

    import com.spring.service.UserService;

    @ContextConfiguration(locations
    ="classpath:applicationContext.xml")
    public class SpringTest extends AbstractJUnit4SpringContextTests {

        @Resource(name
    ="userService")
        
    private UserService userService;
        
        @Test
        
    public void test1(){
            userService.say();
            System.out.println();
            userService.smile();
            System.out.println();
            userService.cry();
        }
        
    }
    運行結果

    ----------before-------------
    say method is called
    ----------after-------------

    ----------before-------------
    smile method is called
    ----------after-------------

    ----------before-------------
    cry method is called
    ----------after-------------

    點我下載工程代碼

    posted on 2010-10-29 11:11 雪山飛鵠 閱讀(2076) 評論(0)  編輯  收藏 所屬分類: spring
    主站蜘蛛池模板: yy6080亚洲一级理论| 亚洲中文字幕无码久久| 嘿嘿嘿视频免费网站在线观看| 亚洲色偷偷综合亚洲AV伊人蜜桃| 伊在人亚洲香蕉精品区麻豆| 久久99毛片免费观看不卡| 亚洲综合欧美色五月俺也去| 自拍偷自拍亚洲精品被多人伦好爽| 中国人xxxxx69免费视频| 国产亚洲福利精品一区二区| 亚洲人成电影亚洲人成9999网 | 国产性生交xxxxx免费| a级片在线免费看| 亚洲成a人无码亚洲成www牛牛| 亚洲乱亚洲乱妇无码麻豆| 免费黄网在线观看| 日本中文字幕免费高清视频| 老子影院午夜伦不卡亚洲| 亚洲国产精品一区| 亚洲一区二区三区免费| 欧亚精品一区三区免费| 国产偷伦视频免费观看| 真人无码作爱免费视频| 亚洲精品亚洲人成在线播放| 亚洲国产精品久久久天堂| 免费人成网站在线高清| 2021国产精品成人免费视频| a色毛片免费视频| 人人公开免费超级碰碰碰视频| 免费观看的av毛片的网站| 久久成人免费电影| 一级片在线免费看| 蜜臀亚洲AV无码精品国产午夜.| 亚洲精品不卡视频| 亚洲AV福利天堂一区二区三 | 亚洲色成人网一二三区| 国产亚洲色视频在线| 亚洲AⅤ永久无码精品AA| 夜夜嘿视频免费看| 无码中文在线二区免费| 亚洲免费观看网站|