<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 雪山飛鵠 閱讀(2077) 評論(0)  編輯  收藏 所屬分類: spring
    主站蜘蛛池模板: 一级毛片直播亚洲| 亚洲成人中文字幕| 97无码人妻福利免费公开在线视频 | 国产成人精品日本亚洲18图| 国产男女性潮高清免费网站| 青青操视频在线免费观看| 亚洲国产成AV人天堂无码| 免费国产真实迷j在线观看| 暖暖免费在线中文日本| 亚洲欧洲无码一区二区三区| 亚洲乱色熟女一区二区三区丝袜| 免费看成人AA片无码视频羞羞网| 免费手机在线看片| 亚洲日韩国产精品无码av| 免费h成人黄漫画嘿咻破解版| 特级无码毛片免费视频尤物| 国产成人综合亚洲绿色| 99久久亚洲精品无码毛片| 亚洲国产91精品无码专区| 黄色网址免费大全| 免费人成网站在线观看不卡 | 一级毛片a女人刺激视频免费| 91嫩草私人成人亚洲影院| 亚洲?V无码乱码国产精品| xxxx日本免费| 伊人久久大香线蕉免费视频| 亚洲爆乳无码专区www| 久久久久亚洲AV无码网站| 亚洲真人日本在线| 永久免费bbbbbb视频| 可以免费看黄的网站| 免费人成网站在线观看不卡| 特级av毛片免费观看| 亚洲精品免费网站| 久久亚洲AV无码精品色午夜麻豆| 亚洲一级Av无码毛片久久精品| 青青青国产免费一夜七次郎| 999国内精品永久免费视频| 久久99热精品免费观看动漫| 久久免费观看视频| 日韩在线视频播放免费视频完整版 |