美文网首页
关于SpringMVC监听器以及生命周期的思考

关于SpringMVC监听器以及生命周期的思考

作者: 浮云骑士_ | 来源:发表于2017-07-03 14:55 被阅读0次

这篇文章旨在记录关于SpringMVC过程中会用到的监听器

1.Servlet监听

用来监听Servlet的生命周期。
主要类:ServletContextListener(监听网站启动过程),
HttpSessionListener(监听客户端会话过程), HttpSessionAttributeListener
实现:
创建类实现以上的接口比如:

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

web.xml配置:

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

2.Bean 初始化监听

可以监听Spring创建Bean的实例的初始化前后
主要类:
BeanPostProcessor
实现:

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主要在于多个同等类时执行的顺序,return返回的是对象,这里可以偷天换日。

SpringContext.xml:

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

3.类的初始化和销毁的监听。

此监听旨在监听bean的自身初始化和销毁过程,初始化的执行方法在第2个监听之后
实现:
第一种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设置。
第二种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>

相关文章

网友评论

      本文标题:关于SpringMVC监听器以及生命周期的思考

      本文链接:https://www.haomeiwen.com/subject/ifkacxtx.html