Java Listener中Spring接口注入的使用
2024-09-06 06:08:29
在项目中使用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,需要注意前后顺序。接下来就是通过开启一个线程,来处理当前的一些数据逻辑。这段代码中另外一个注意的地方就是有时,我们在想如何将一个参数传递到一个线程中, 上文我的做法是通过构造函数传入。
最新文章
- 解决Python中不能输入汉字的问题
- 为什么类和接口不能使用private和protected?接口的方法不能使用private、protected、default
- 【JavaScript】ES6 新语法
- spark 你要喧宾夺主么?好好干。
- structs常见错误
- android ListView嵌套GridView显示不全问题
- 再谈this
- ios UIWebView截获html并修改便签内容(转载)
- ListView为什么用setOnItemClick这个方法和onTouch这个方法有冲突
- 小白日记29:kali渗透测试之Web渗透-扫描工具-Vega
- 如何测试 Android 中的定时事件
- 数据结构(启发式合并):HNOI 2009 梦幻布丁
- Uva227.Puzzle
- 关于arguments.callee的用途
- Ubuntu 问题解决汇总
- 2012-11-17 12:28 用MFC实现的计算器(详细版)
- 阿里云服务器 通过JavaMail发送邮箱STMP问题( 25端口被禁用 使用SSL协议465端口 )
- Log4j.properties 配置详解
- SQLServer版本
- elixir mix 简介
热门文章
- Python之内置函数一
- samba服务和nginx服务
- 【转】PHP实现验证码
- IPython:一种交互式计算和开发环境
- ACM-ICPC 2018 徐州赛区网络预赛 C Cacti Lottery(暴力+期望)
- Android APP测试流程
- 使用hibernate从一方获取多方信息时报错:org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
- gitlab常用命令
- WordPaster-KesionCMS V9整合教程
- ckeditor粘帖上传图片控件-更新-2.0.15版本