這篇文章旨在記錄關(guān)于SpringMVC過程中會(huì)用到的監(jiān)聽器
1.Servlet監(jiān)聽
用來監(jiān)聽Servlet的生命周期。
主要類:ServletContextListener(監(jiān)聽網(wǎng)站啟動(dòng)過程),
HttpSessionListener(監(jiān)聽客戶端會(huì)話過程), HttpSessionAttributeListener
實(shí)現(xiàn):
創(chuàng)建類實(shí)現(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的實(shí)例的初始化前后
主要類:
BeanPostProcessor
實(shí)現(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主要在于多個(gè)同等類時(shí)執(zhí)行的順序,return返回的是對(duì)象,這里可以偷天換日。
SpringContext.xml:
<bean id="testpost" class="....TestProcess"></bean>
3.類的初始化和銷毀的監(jiān)聽。
此監(jiān)聽旨在監(jiān)聽bean的自身初始化和銷毀過程,初始化的執(zhí)行方法在第2個(gè)監(jiān)聽之后
實(shí)現(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>