好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot中使用监听器的方法详解

1.监听器

web监听器是一张Servlet中特殊的类,它们能帮助开发者监听web中特定的事件,比如ServletContext,HttpSession,ServletRequest的创建和销毁;变量的创建、销毁、和修改等。可以在某些动作前后增加处理,实现监控

2.SpringBoot中监听器的使用

web监听器的使用场景很多,比如监听servlet上下文用来初始化一些数据、监听http session用来获取当前在线的人数、监听客户端请求的servletrequest对象来获取用户的访问信息等。

2.1 监听Servlet上下文对象

监听Servlet上下文对象可以用来初始化数据,用于缓存。举个例子:比如用户在点击某个网站的首页时,一般都会展现出首页的一些信息,而这些信息基本上或者大部分时间都保持不变的,但是这些信息都是来自数据库,如果用户的每次点击,都要从数据库中去获取数据的话,用户量少还可以接受,如果用户量非常大的话,这对数据库也是一笔很大的开销。

针对这种首页数据,大部分都不常更新的话,我们完全可以把它们缓存起来,每次用户点击的时候,我们都直接从缓存中拿,这样既可以提高首页的访问速度,又可以降低服务器的压力,如果做得更加灵活一点,可以再加个定时器,定期的来更新这个首页缓存,就类似于csdn个人博客首页中排名的变化一样

下面我们针对这个功能,来写一个Service,模拟一下从数据库查询数据:

?

1

2

3

4

5

6

7

8

9

10

package com.example.springdemo1.service;

import com.example.springdemo1.pojo.User;

import org.springframework.stereotype.Service;

@Service

public class UserService2 {

     public User getUser(){

         //实际中会根据具体的业务场景,从数据库中查询对应的信息

         return new User( 10 , "lyh10" , "123456" );

     }

}

然后写一个监听器,实现ApplicationListener接口,重写onApplicationEvent方法,将ContextRefreshedEvent对象传进去,如果我们想在加载或刷新应用上下文时,也重新刷新下我们预加载的资源,就可以通过监听ContextRefreshedEvent来做这样的事情,如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

package com.example.springdemo1.util;

import com.example.springdemo1.pojo.User;

import com.example.springdemo1.service.UserService2;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationListener;

import org.springframework.context.event.ContextRefreshedEvent;

import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;

@Component

public class MyServletContextListener implements ApplicationListener<ContextRefreshedEvent> {

     @Override

     public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

         //先获取到application上下文

         ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();

         //获取对应的Service

         UserService2 userService2 = applicationContext.getBean(UserService2. class );

         User user = userService2.getUser();

         //获取application域对象,将查到的信息放到application域中

         ServletContext application = applicationContext.getBean(ServletContext. class );

         application.setAttribute( "user" ,user);

     }

}

首先通过contextRefreshedEvent来获取application上下文,再通过application上下文来获取UserService这个bean,然后再调用自己的业务代码获取相应的数据,最后存储到application域中,这样前端在请求相应数据的时候,我们就可以直接从application域中获取信息,减少数据库的压力,下面写一个controller直接从application域中获取user信息来测试一下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.example.springdemo1.controller;

import com.example.springdemo1.pojo.User;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

@RestController

@RequestMapping ( "/testListener" )

public class testController12 {

     @GetMapping ( "/user" )

     public User getUser(HttpServletRequest request){

         ServletContext application = request.getServletContext();

         return (User)application.getAttribute( "user" );

     }

}

启动项目,在浏览器中输 http://localhost:8082/testListener/user 测试一下即可,如果正常返回user信息,那么说明数据已经缓存成功,不过application这种事缓存在内存中,对内存会有消耗。

2.2 监听HTTP会话Session对象

监听器还有一个比较常用的地方就是用来监听session对象,来获取在线用户数量。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

package com.example.springdemo1.util;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

@Component

public class MyHttpSessionListener implements HttpSessionListener {

     private static Logger logger = LoggerFactory.getLogger(MyHttpSessionListener. class );

     //记录在线的用户数量

     public Integer count = 0 ;

     @Override

     public synchronized void sessionCreated(HttpSessionEvent httpSessionEvent){

         logger.info( "新用户上线了" );

         count++;

         httpSessionEvent.getSession().getServletContext().setAttribute( "count" ,count);

     }

     @Override

     public synchronized void sessionDestroyed(HttpSessionEvent httpSessionEvent){

         logger.info( "用户下线了" );

         count--;

         httpSessionEvent.getSession().getServletContext().setAttribute( "count" ,count);

     }

}

可以看出,首先该监听器需要实现HttpSessionListener接口,然后重写sessionCreated和sessionDestroyed方法,在sessionCreated方法中传递一个httpSessionEvent对象,然后将当前session中的用户数量加一,sessionDestroyed方法方法刚好相反,不再赘述,然后我们再写一个controller来测试一下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package com.example.springdemo1.controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController

@RequestMapping ( "/testMyListener" )

public class testController13 {

     /**

      * 获取当前在线人数,该方法有bug

      * @param request

      * @return

      */

     @GetMapping ( "/total" )

     public String getTotalUser(HttpServletRequest request) {

         Integer count = (Integer) request.getSession().getServletContext().getAttribute( "count" );

         return "当前在线人数:" + count;

     }

}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!  

原文链接:https://blog.csdn.net/liyuuhuvnjjv/article/details/123454133

查看更多关于SpringBoot中使用监听器的方法详解的详细内容...

  阅读:13次