Java Listener中Spring接口注入的使用

时间:2023-03-09 19:46:17
Java Listener中Spring接口注入的使用

在项目中使用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,需要注意前后顺序。接下来就是通过开启一个线程,来处理当前的一些数据逻辑。这段代码中另外一个注意的地方就是有时,我们在想如何将一个参数传递到一个线程中, 上文我的做法是通过构造函数传入。

相关文章