spring2.5 dbcp连接数据库所需jar包: spring.jar ,commons-pool-xxx.jar ,commons-dbcp-xxx.jar,commons-collections-xxx.jar. 其中xxx为版本号。
如果需要加入任务计划Quartz,所需要的jar包为
配置文件在src平级目录conf下,可以采用FileSystemXmlApplicationContext 来读取文件,如:
private static ApplicationContext applicationContext = new FileSystemXmlApplicationContext("conf/applicationContext.xml");
private static GateDao dao = (GateDao)applicationContext.getBean("gateDao");
配置文件在src下,可以采用ClassPathXmlApplicationContext 来载入文件,如:
private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
private static GateDao dao = (GateDao)applicationContext.getBean("gateDao");
任务的配置文件applicationContext-task.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--任务执行bean--> <bean name="taskBean" class="com.fsti.newdev.schedule.TaskBean"> <property name="dao"> <ref bean="gateDao" /> </property> <property name="gateService"> <ref bean="gateService" /> </property> </bean> <!--同步时间调用的方法JobDetail--> <bean id="syncTime" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <ref bean="taskBean" /> </property> <property name="targetMethod"> <value>syncTime</value> </property> <property name="concurrent" value="false"/> </bean> <!-- 同步时间的触发器Trigger--> <bean id="cronSyncTimeTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="syncTime" /> </property> <property name="cronExpression"> <!--<value>0/10 * * * * ?</value>--> <value>0 30 0 * * ?</value> <!--每隔一天同步时间--> </property> </bean> <!-- Quartz Scheduler 任务列表--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- --> <ref bean="cronSyncTimeTrigger" /> </list> </property> </bean>
执行任务的TaskBean:
package com.fsti.newdev.schedule;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.fsti.newdev.dao.GateDao;
import com.fsti.newdev.model.BaseDev;
import com.fsti.newdev.model.ListBean;
import com.fsti.newdev.service.GateService;
import com.fsti.newdev.util.StrUtil;
public class TaskBean {
private static final Logger logger = Logger.getLogger(TaskBean.class);
private GateDao dao;
private GateService gateService;
/**
* 开始同步门禁控制器时间
*/
public void syncTime(){
logger.info("开始同步门禁时间");
List<BaseDev> gates = new ArrayList<BaseDev>();
try {
logger.info("开始查询门禁设备列表");
gates = dao.getDevices();
logger.info("查询到的门禁设备个数:" + gates.size());
} catch (SQLException e) {
logger.error("查询门禁设备列表出现异常",e);
return;
}
for(BaseDev gate : gates) {
logger.info("同步门禁时间:设备编号="+gate.getStationNo()+
",设备ip="+gate.getIp());
int i = gateService.setDateTime(gate.getStationNo(),gate.getIp());
if(i==1) {
logger.info("同步门禁时间成功:设备编号="+gate.getStationNo());
} else {
logger.info("同步门禁时间失败:设备编号="+gate.getStationNo());
}
}
}
public GateDao getDao() {
return dao;
}
public void setDao(GateDao dao) {
this.dao = dao;
}
public GateService getGateService() {
return gateService;
}
public void setGateService(GateService gateService) {
this.gateService = gateService;
}
}
最后,Main函数直接载入xml配置文件即可自动调用任务:
package com.fsti.newdev;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Main {
/**
* @param args
*/
@SuppressWarnings("unused")
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("conf/applicationContext-task.xml");
}
}