在项目中使用Spring通常使用他的依赖注入可以很好的处理,接口与实现类之间的耦合性,但是通常的应用场景中都是Service层和DAO层,或者web层的话, 也是与Strust2来整合,那么如何在Listener中使用Spring自动注入的接口呢。 接下来开始记录下今天做的一个小工具。

这个小工具是通过这个Listener来开启一个线程, 然后定时访问数据库中的数据,将数据获取出来后,然后逐条分析数据里边的手机号码,来通过淘宝提供的一个接口来分析手机号的归属地。那么在Listener中如何来访问数据库呢。下面直接贴代码:

...

import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import cn.org.rapid_framework.page.Page; /**
* Listener的方式在后台执行一线程
*
* @author
*
*/
public class MyListener implements ServletContextListener {
private MyThread myThread;
private Logger logger = Logger.getLogger(MyListener.class); public void contextDestroyed(ServletContextEvent e) {
if (myThread != null && myThread.isInterrupted()) {
myThread.interrupt();
}
} public void contextInitialized(ServletContextEvent e) {
String str = null;
if (str == null && myThread == null) {
myThread = new MyThread(e);
myThread.start(); // servlet 上下文初始化时启动 socket
}
}
} /**
* 自定义一个 Class 线程类继承自线程类,重写 run() 方法,用于从后台获取并处理数据
*
* @author Champion.Wong
*
*/
class MyThread extends Thread {
private Logger logger = Logger.getLogger(MyThread.class);
ServletContextEvent _e;
public MyThread(ServletContextEvent e) {
_e = e;
} public void run() {
while (!this.isInterrupted()) {// 线程未中断执行循环
IGoldenPhoneManager phoneManager;
List<GoldenPhone> phoneList = new ArrayList<GoldenPhone>();
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(_e.getServletContext());
phoneManager = (IGoldenPhoneManager) context.getBean("igoldenPhoneManager");
System.out.println("开始获取所有电话记录...");
int pageSize = 100;
for (int i = 0; i < 46800; i++) {
GoldenPhoneQuery query = new GoldenPhoneQuery();
query.setPageNumber(i);
query.setPageSize(pageSize);
Page result = phoneManager.findPage(query);
if (result != null) {
phoneList = result.getResult();
System.out.println("获取所有电话记录数目:" + result.getResult().size());
if (!CollectionUtils.isEmpty(phoneList)) {
for (GoldenPhone phoneModel : phoneList) {
if (StringUtils.isBlank(phoneModel.getProvinces())) {
String provinces = new PhoneService().getPhoneArea(phoneModel.getTel());
phoneModel.setProvinces(provinces);
phoneManager.update(phoneModel);
try {
Thread.sleep(3000);
System.out.println("休息3m钟...");
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
}
}
}
}
}

可以看到,方法中通过

WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(_e.getServletContext()); phoneManager = (IGoldenPhoneManager) context.getBean("igoldenPhoneManager"); 

来获取一个bean,这样就能获取到一个已经注入好的bean了。这里需要注意的是:在web.xml中添加这个侦听的时候, 需要先引入Spring的Listener然后引入这个自定义的Listener,需要注意前后顺序。接下来就是通过开启一个线程,来处理当前的一些数据逻辑。这段代码中另外一个注意的地方就是有时,我们在想如何将一个参数传递到一个线程中, 上文我的做法是通过构造函数传入。

Java Listener中Spring接口注入的使用的更多相关文章

  1. 在网页程序或Java程序中调用接口实现短信猫收发短信的解决方案

    方案特点: 在网页程序或Java程序中调用接口实现短信猫收发短信的解决方案,简化软件开发流程,减少各应用系统相同模块的重复开发工作,提高系统稳定性和可靠性. 基于HTTP协议的开发接口 使用特点在网页 ...

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

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

  3. 轻松理解 Java开发中的依赖注入(DI)和控制反转(IOC)

    前言 关于这个话题, 网上有很多文章,这里, 我希望通过最简单的话语与大家分享. 依赖注入和控制反转两个概念让很多初学这迷惑, 觉得玄之又玄,高深莫测. 这里想先说明两点: 依赖注入和控制反转不是高级 ...

  4. java类中定义接口

    今天看到一个java类中定义了接口,写个备忘录,记录一下 package com.gxf.test; public class Test_interface { public interface sh ...

  5. JAVA入门[3]—Spring依赖注入

    Spring支持属性注入和构造器注入,它支持XML和注解两种方式.本文介绍Spring控制反转容器加载包含beans的XML文件,实现依赖注入. 一.创建bean实例 暂且抛开对象依赖,我们先看下如何 ...

  6. listener中@Autowired无法注入bean的一种解决方法

    背景:使用监听器处理业务,需要使用自己的service方法: 错误:使用@Autowired注入service对象,最终得到的为null: 原因:listener.fitter都不是Spring容器管 ...

  7. Java集合中Map接口的使用方法

    Map接口 Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value: Map中的键值对以Entry类型的对象实例形式存在: 建(key值 ...

  8. (2) Java SQL框架(java.sql.*)中常用接口详解

    Driver接口:定义了一个驱动程序接口,每一个数据库的JDBC driver都应该实现这个接口,用于访问对应的数据库.比如MySQL的driver为com.mysql.jdbc.Driver.Jav ...

  9. Java开发中模拟接口工具moco的使用

    场景 在开发中需要依赖一些接口,比如需要请求一个返回Json数据的接口,但是返回Json数据的接口要么是没搭建,要么是交互比较复杂. 此时,就可以使用moco来模拟接口返回接口数据,以便开发和测试工作 ...

随机推荐

  1. vue2.0 移动端,下拉刷新,上拉加载更多插件 转:

    转自:http://www.cnblogs.com/gmajip/p/7204072.html <template lang="html"> <div class ...

  2. Bootstrap之javascript组件

    一 模态框 模态框放在body标签里面的,是body标签的子元素 静态实例: <div class="modal fade" tabindex="-1" ...

  3. Linux 第三天

    2.文件处理命令 1)touch 创建空文件 语法:touch文件名 2)cat 显示文件内容 英文原意:concatenate 语法:cat 文件名 常用选项: -n:number,显示行号 3)t ...

  4. springboot xml声明式事务管理方案

    在开发过程中springboot提供的常见的事务解决方案是使用注解方式实现. 使用注解 在启动类上添加注解 @EnableTransactionManagement 在需要事务控制的方法添加@Tran ...

  5. Codeforces Round #543 (Div. 2) D 双指针 + 模拟

    https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...

  6. Remote Debugging (1)

    The client/server design of the Java debugger allows you to launch a Java program from computer on y ...

  7. Docker Swarm集群部署

    一.系统环境 1)服务器环境 节点名称 IP 操作系统 内核版本 manager 172.16.60.95 CentOs7 4.16.1-1.el7.elrepo.x86_64 node-01 172 ...

  8. codehouse

    1 # 整数部分十进制转二进制 2 3 num = int(raw_input(">>>")) 4 5 if num < 0: 6 isNeg = True ...

  9. 自定义cell的高度

    // //  RootTableViewController.m //  Share // //  Created by lanouhn on 15/1/20. //  Copyright (c) 2 ...

  10. 绑定弹窗事件最好的方法,原生JS和JQuery方法

    使用jQuery ui = { $close: $('.close') , $pop: $('.pop') , $topopBtn: $('.topop-btn') , $popbtnArea: $( ...