zookeeper python接口实例详解
295
2023-01-18
详解springboot整合Listener的两种方式
1.通过注解
编写启动类
package cn.bl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
编写一个监听器
package cn.bl.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class FirstListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("init .. ");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("desroyed .. ");
}
}
当执行App的时候
2.通过函数
package cn.bl.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("second servletListener init .. ");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("second servletListener destroy .. ");
}
}
package cn.bl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotatiohttp://n.Bean;
import cn.bl.listener.SecondListener;
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Bean
public ServletListenerRegistrationBean
ServletListenerRegistrationBean&lmcKqJt;SecondListener>bean = new ServletListenerRegistrationBean<>(new SecondListener());
return bean;
}
}
总结
以上所述是给大家介绍的springboot整合Listener的两种方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,会及时回复大家的。在此也非常感谢大家对我们网站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~