關(guān)于SpringMVC監(jiān)聽器以及生命周期的思考

這篇文章旨在記錄關(guān)于SpringMVC過程中會用到的監(jiān)聽器

1.Servlet監(jiān)聽

用來監(jiān)聽Servlet的生命周期。
主要類:ServletContextListener(監(jiān)聽網(wǎng)站啟動過程),
HttpSessionListener(監(jiān)聽客戶端會話過程), HttpSessionAttributeListener
實現(xiàn):
創(chuàng)建類實現(xiàn)以上的接口比如:

public class TestListener implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {
    .....
}

web.xml配置:

<listener>
        <listener-class>com.test.TestListener</listener-class>
</listener>

2.Bean 初始化監(jiān)聽

可以監(jiān)聽Spring創(chuàng)建Bean的實例的初始化前后
主要類:
BeanPostProcessor
實現(xiàn):

public class TestProcess implements BeanPostProcessor,Ordered {
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("postProcessBeforeInitialization");
        return o;
    }

    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("postProcessAfterInitialization");
        return o;
    }


    public int getOrder() {
        return 1;
    }
}

Order主要在于多個同等類時執(zhí)行的順序,return返回的是對象,這里可以偷天換日。

SpringContext.xml:

<bean id="testpost" class="....TestProcess"></bean>

3.類的初始化和銷毀的監(jiān)聽。

此監(jiān)聽旨在監(jiān)聽bean的自身初始化和銷毀過程,初始化的執(zhí)行方法在第2個監(jiān)聽之后
實現(xiàn):
第一種1.SpringContext.xml

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!"/>
</bean>

通過init-method和destroy-method設(shè)置。
第二種2.在bean中

@PostConstruct
public void init(){
System.out.println("Bean is going through init.");
}
@PreDestroy
public void destroy(){
System.out.println("Bean will destroy now.");
}

4.AOP。

面向切面編程
第一種方式:
SpringContext.xml:

<aop:config>
        <aop:aspect id="myaspect" ref="peoAsp">
            <aop:pointcut  id="mypointcut" expression="execution(* jis.*.*(..))"/>
            <aop:before method="beforeAdvice" pointcut-ref="mypointcut"/>
            <aop:after-throwing method="AfterThrowingAdvice" pointcut-ref="mypointcut" throwing="ex"/>
            <aop:after-returning method="afterReturningAdvice" pointcut-ref="mypointcut" returning="retVal"/>
        </aop:aspect>
    </aop:config>

第二種方式:
Bean中:

@Aspect
public class PeoAsp  {

    public PeoAsp(){}

    @Pointcut("execution(* com.jis.People.aspTest(..))")
    public void selectAll(){};

    /**
     * This is the method which I would like to execute
     * before a selected method execution.
     */
    @Before("selectAll()")
    public void beforeAdvice(){
        System.out.println("Going to setup student profile.");
    }
    /**
     * This is the method which I would like to execute
     * after a selected method execution.
     */
    @After("selectAll()")
    public void afterAdvice(){
        System.out.println("Student profile has been setup.");
    }
    /**
     * This is the method which I would like to execute
     * when any method returns.
     */
    @AfterReturning(pointcut = "selectAll()",returning = "retVal")
    public void afterReturningAdvice(Object retVal){
        System.out.println("Returning:" + retVal.toString() );
    }
    /**
     * This is the method which I would like to execute
     * if there is an exception raised.
     */
    public void AfterThrowingAdvice(Exception ex){
        System.out.println("There has been an exception: " + ex.toString());
    }


}

并在SpringContext中配置:
<aop:aspectj-autoproxy/>
<bean class="com.jis.PeoAsp" id="peoAsp"></bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • 從三月份找實習到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍閱讀 42,851評論 11 349
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,811評論 18 399
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,283評論 6 342
  • 最近開始看前幾年比較流行的電視劇《裸婚時代》,男女主人公分別是文章飾演的劉易陽和姚笛飾演的童佳倩。我內(nèi)心...
    不想變懶的豬閱讀 1,864評論 2 1

友情鏈接更多精彩內(nèi)容