springboot基础-监听器,过滤器,拦截器,aop,自定义注解
环境
idea2018,jdk1.8,
springboot版本:1.5.9.RELEASE
代码下载:
https://github.com/2010yhh/springBoot-demos.git
测试:启动项目后,访问:
http://localhost:8080/springboot-demo2/test1
http://localhost:8080//springboot-demo2/test2
1.监听器
listen的作用:可以在listen中完成一些如数据库、创建、数据库加载等一些初始化操作
@Component
public class StartApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
public static AtomicInteger count=new AtomicInteger(0);
protected Logger log = LoggerFactory.getLogger(StartApplicationListener.class);
private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//防止重复执行
if (event.getApplicationContext().getParent() == null && count.incrementAndGet()<=1) {
//这里一个定时任务的初始化
this.service.scheduleAtFixedRate(new LogTask(),1000, 1000*60,TimeUnit.MILLISECONDS );