<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
    所需jar包

    編寫dao
    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");
        }
    }
    注意觀察包名。@Component("userDAO")等價于在spring配置文件中定義一個<bean id="userDAO"/>
    編寫Service
    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();
        }
    }
    注意觀察包名。@Component("userService")等價于在spring配置文件中定義一個<bean id="userService"/> @Resource(name="userDAO")將userDA注入進來
    寫一個攔截器的類
    package com.spring.aop;

    import org.springframework.stereotype.Component;

    @Component(
    "logIntercepter")
    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-------------");
        }
    }
    注意觀察包名。@Component("logIntercepter")等價于在spring配置文件中定義一個<bean id="logIntercepter"/>
    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: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.*"/>
    兩行為開啟spring的注解配置
    <aop:aspect id="aspect" ref="logIntercepter"> 引入具體的AOP操作類
    <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/>聲明一個切入點,注意execution表達式的寫法
    <aop:before method="before" pointcut-ref="pointCut"/> aop前置通知
    <aop:after method="after" pointcut-ref="pointCut"/> aop后置通知,
    <aop:after-throwing method="exception" pointcut-ref="pointCut"/> aop異常通知
    以上結合起來意思就是在調用com.spring.service包或子包下的所有方法之前或之后或拋出異常時依次調用id為logIntercepter的類中的before after exception方法
    測試用例
    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();
        }
        
    }
    此單元測試基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的關于單元測試的支持
    在類上標注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路徑下加載applicationContext.xml
    @Resource(name="userService")意思是把userService注入進來

    最終輸出結果為:

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

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

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




    點我下載工程代碼
    posted on 2010-10-29 10:36 雪山飛鵠 閱讀(2642) 評論(0)  編輯  收藏 所屬分類: spring
    主站蜘蛛池模板: 9久9久女女免费精品视频在线观看 | 国产精品亚洲аv无码播放| 亚洲国产精品精华液| 免费不卡视频一卡二卡| 亚洲综合小说久久另类区| 免费一级不卡毛片| 永久亚洲成a人片777777| 无码毛片一区二区三区视频免费播放| 成人毛片18女人毛片免费96| 亚洲人成日本在线观看| **真实毛片免费观看| 亚洲精品私拍国产福利在线| 丁香花在线视频观看免费| 亚洲中文字幕无码中文字在线| 日本特黄特色AAA大片免费| 国产成人免费全部网站| 欧美亚洲国产SUV| 国产无遮挡裸体免费视频| 亚洲国产aⅴ成人精品无吗| 成人免费视频网址| 亚洲最大中文字幕无码网站| 在线观看日本免费a∨视频| 亚洲AV成人噜噜无码网站| 91频在线观看免费大全| 国产成人精品日本亚洲直接| 黄页网站免费在线观看| 中文文字幕文字幕亚洲色| 久久精品网站免费观看| 自拍偷区亚洲国内自拍| 免费a级毛片无码a∨蜜芽试看| 亚洲免费福利在线视频| 午夜一区二区免费视频| 噜噜综合亚洲AV中文无码| 免费人妻av无码专区| 人妻巨大乳hd免费看| 国产亚洲精品无码拍拍拍色欲| 成人免费ā片在线观看| 久久久久亚洲AV成人无码| 日韩免费高清大片在线 | 亚洲AV无码片一区二区三区| 四虎永久免费地址在线观看|