背景:使用监听器处理业务,需要使用自己的service方法;

错误:使用@Autowired注入service对象,最终得到的为null;

原因:listener、fitter都不是Spring容器管理的,无法在这些类中直接使用Spring注解的方式来注入我们需要的对象。

解决:写一个bean工厂,从spring的上下文WebApplicationContext 中获取。

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* @ClassName: SpringJobBeanFactory*/
@Component
public class SpringJobBeanFactory implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringJobBeanFactory.applicationContext=applicationContext; }
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) throws BeansException {
if (applicationContext == null){
return null;
}
return (T)applicationContext.getBean(name);
} public static <T> T getBean(Class<T> name) throws BeansException {
if (applicationContext == null){
return null;
}
return applicationContext.getBean(name);
}
}

监听器里获取service,使用getBean(XXX.class)

@Override
public void sessionDestroyed(HttpSessionEvent event) {
logger.debug("session销毁了");
UserCountUtils.subtract();//在线人数-1 HttpSession session = event.getSession();// 获得Session对象
ServletContext servletContext = session.getServletContext(); Account acc = (Account) session.getAttribute("user");
if(acc!=null){
@SuppressWarnings("unchecked")
Map<String, String> loginMap = (Map<String, String>) servletContext.getAttribute("loginMap");
loginMap.remove(acc.getUserName());
servletContext.setAttribute("loginMap", loginMap);
session.removeAttribute("user");
acc.setOnlineState(false); //注销成功修改用户在线状态为 离线
//WebApplicationContext appctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//AccountService accountService = appctx.getBean(AccountService.class);
AccountService accountService = SpringJobBeanFactory.getBean(AccountService.class);
accountService.updateAccount(acc);
logger.debug(acc.getUserName()+"用户注销!");
}

listener中@Autowired无法注入bean的一种解决方法的更多相关文章

  1. Listener中@Autowired无法注入的问题

    最近在用监听器的时候遇到了spring无法注入的问题,代码如下,这个task总是null,包明明已经被扫到了,就是注入不进来. public class MyListener implements S ...

  2. Springboot中关于跨域问题的一种解决方法

    前后端分离开发中,跨域问题是很常见的一种问题.本文主要是解决 springboot 项目跨域访问的一种方法,其他 javaweb 项目也可参考. 1.首先要了解什么是跨域 由于前后端分离开发中前端页面 ...

  3. 关于八数码问题中的状态判重的三种解决方法(编码、hash、&lt;set&gt;)

    八数码问题搜索有非常多高效方法:如A*算法.双向广搜等 但在搜索过程中都会遇到同一个问题.那就是判重操作(假设反复就剪枝),怎样高效的判重是8数码问题中效率的关键 以下关于几种判重方法进行比較:编码. ...

  4. fluent中UDF环境变量问题的三种解决方法

    方法一: 这种方式最简便,首选这种,但是有时会因为不明原因而不好使,我自己电脑刚开始用这种方式是行得通的,但是后来中途装过很多乱七八糟的软件,估计环境变量改乱了,这时候只能用第二种或者第三种方法.先说 ...

  5. spring 注入bean的两种方式

    我们都知道,使用spring框架时,不用再使用new来实例化对象了,直接可以通过spring容器来注入即可. 而注入bean有两种方式: 一种是通过XML来配置的,分别有属性注入.构造函数注入和工厂方 ...

  6. 检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下:

    检索 COM 类工厂中 CLSID 为 {10020200-E260-11CF-AE68-00AA004A34D5} 的组件时失败,解决方法如下: 第 一步:首先将msvcr71.dll,  SQLD ...

  7. log4j中Spring控制台输出Debug级信息过多解决方法

    log4j中Spring控制台输出Debug级信息过多解决方法 >>>>>>>>>>>>>>>>> ...

  8. 安卓(android)建立项目时失败,出现Android Manifest.xml file missing几种解决方法?(总结中)

    安卓(android)建立项目时失败.出现AndroidManifest.xml file missing几种解决方法?(总结中) Eclipse新建项目.遇到这种问题.注意例如以下: 1.文件名称最 ...

  9. universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法

    在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...

随机推荐

  1. 《linux就该这么学》课堂笔记15 vsftpd文件传输、Samba/NFS文件共享

    1.为了能够在如此复杂多样的设备之间(Windows.Linux.UNIX.Mac等不同的操作系统)解决问题解决文件传输问题,文件传输协议(FTP)应运而生. FTP服务器是按照FTP协议在互联网上提 ...

  2. Go Programming Language 2

    [Go Programming Language 2] 1.In Go, the sign of the remainder is always the same as the sign of the ...

  3. scapy构造数据包

    一.进入scapy交互界面 在终端下输入:scapy ,进入交互界面: 二.查看scapy已经实现的网络协议 ls() 列出scapy中已实现的网络协议 ls(协议类型) 查看某个协议头部字段格式 l ...

  4. Nginx on Docker 配置

    docker run -d -p 8082:8082 --name two-nginx -v ~/workplace/nginx/html:/usr/share/nginx/html -v ~/wor ...

  5. LeetCode 1123. Lowest Common Ancestor of Deepest Leaves

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ 题目: Given a rooted b ...

  6. 创建、查看、删除计划任务at命令举例

    1.三天后的下午 5 点执行 /bin/ls : at 5pm + 3 days at> /bin/ls             结束按ctrl+d 查看计划任务:at -l 之后 at -c ...

  7. go.rice 强大灵活的golang 静态资源嵌入包

    以前简单介绍过packr ,statik 等静态资源嵌入工具包的使用,go.rich 是一个与packr 类似的静态资源嵌入包,使用简单 功能强大 项目结构 golang mod   go mod i ...

  8. TensorFlow多层感知机函数逼近过程详解

    http://c.biancheng.net/view/1924.html Hornik 等人的工作(http://www.cs.cmu.edu/~bhiksha/courses/deeplearni ...

  9. Redis NOAUTH Authentication required

    redis设置密码后停止服务报错,NOAUTH Authentication required 可以修改/etc/init.d/redis文件中的stop命令 $CLIEXEC -p $REDISPO ...

  10. LESS是一个CSS预处理器,跨浏览器友好,提供诸如变量,函数, mixins 和操作等功能,可以构建动态CSS

    什么是LESS? LESS是一个CSS预处理器,可以为网站启用可自定义,可管理和可重用的样式表. LESS是一种动态样式表语言,扩展了CSS的功能. LESS也是跨浏览器友好. CSS预处理器是一种脚 ...