1.主要是配置文件:如下:(这里说明一下主要是看红色部分的配置,其他的可以根据自己的实际情况修改,这里只是个思路。)
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<!-- 分散配置 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 组件扫描,此处的实现原理是递归扫描所有包,效率较差。开发模式写法如下。后期调优可以将具体包名全部列到下面,以逗号隔开 -->
<context:component-scan base-package="cn.gov.csrc.*"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverclass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
<property name="maxIdleTime" value="${cpool.maxIdleTime}" />
</bean>
<!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- hibernate自身属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- 根据Bean自动生成数据库表,MySql5.5以后要使用org.hibernate.dialect.MySQL5InnoDBDialect才能创建成功! -->
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<!-- 开启二级缓存,其实hibernate默认就是开启的,这里显示的指定一下 -->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.autoReconnect">true</prop>
<!-- 指定二级缓存产品的提供商 -->
<!--
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
-->
</props>
</property>
<property name="packagesToScan">
<list>
<value>cn.gov.csrc.base.systemmanager.model</value>
<value>cn.gov.csrc.report.model</value>
<value>cn.gov.csrc.noreal.report.model</value>
</list>
</property>
</bean>
<!-- hibernate事务管理器,在service层上实现事务管理,达到平台无关性 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="globalRollbackOnParticipationFailure" value="false" />
</bean>
<!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<bean id="genericLogger" class="cn.gov.csrc.base.log.GenericLogger"/>
<!-- aop配置 -->
<aop:config>
<!-- 切入点通知 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.gov.csrc.base.security..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))"/>
</aop:config>
<aop:config>
<aop:pointcut id="logger"
expression="execution(* *..*Dao.save*(..))
or execution(* *..*Dao.update*(..))
or execution(* *..*Dao.delete*(..))
or execution(* *..*Dao.batch*(..))" />
<aop:aspect id="loggerAspect" ref="genericLogger">
<aop:around pointcut-ref="logger" method="invoke" />
</aop:aspect>
</aop:config>
<!-- 任务计划 -->
<!-- 要调用的工作 -->
<bean id="timerAction" class="cn.gov.csrc.report.action.TimerAction"></bean>
<bean id="praseXmlAction" class="cn.gov.csrc.report.action.PraseXmlAction"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="timerAction"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>start</value>
</property>
<!-- 作业不并发调度 -->
<property name="concurrent" value="false"/>
</bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="praseXmlAction"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>parseXml</value>
</property>
<!-- 作业不并发调度 -->
<property name="concurrent" value="false"/>
</bean>
<!-- 定义导出数据到xml的触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 每天晚上11点59分钟59秒执行一次 -->
<!-- <value>0 59 23 * * ?</value> -->
<value>0 23 14 * * ?</value>
</property>
</bean>
<!-- 定义解析xml的触发时间 -->
<bean id="doPraseXml" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 24 14 * * ?</value>
</property>
</bean>
<!-- 总管理类,如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
<ref bean="doPraseXml"/>
</list>
</property>
</bean>
</beans>
package cn.gov.csrc.report.action; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.sql.Blob; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Set; import javax.annotation.Resource; import org.apache.struts2.convention.annotation.Action; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.annotation.Scope; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Controller; import cn.gov.csrc.base.systemmanager.model.User; import cn.gov.csrc.report.model.Accessory; import cn.gov.csrc.report.model.Case; import cn.gov.csrc.report.model.ReportedPerson; import cn.gov.csrc.report.service.CaseService; @Controller() @Scope("prototype") @Action("TimerAction") public class TimerAction extends QuartzJobBean { private int timeout; public TimerAction() { } /** 调度工厂实例化后,经过timeout时间开始执行调度 */ public void setTimeout(int timeout) { this.timeout = timeout; } @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("定时任务执行中......"); } /** * 定时导出外网数据到指定目录 * @throws Exception */ public void start() throws Exception { System.out.println("定时任务执行成功......"); }}
<?xml version="1.0" encoding="GBK"?> <crsc> <data> <举报信息反馈> <R index="1"> <举报编号>1</举报编号> <状态>1</状态> <答复意见>填写答复意见</答复意见> </R> <R index="2"> <举报编号>2</举报编号> <状态>2</状态> <答复意见>填写答复意见</答复意见> </R> <R index="3"> <举报编号>3</举报编号> <状态>3</状态> <答复意见>填写答复意见</答复意见> </R> <R index="4"> <举报编号>4</举报编号> <状态>1</状态> <答复意见>填写答复意见</答复意见> </R> </举报信息反馈> </data> </crsc>
package cn.gov.csrc.report.action; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.apache.struts2.convention.annotation.Action; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.context.annotation.Scope; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.stereotype.Controller; @Controller() @Scope("prototype") @Action("PraseXmlAction") public class PraseXmlAction extends QuartzJobBean { private static final String xmlPath = "D:\\admin\\" + getFileName() + "\\case.xml"; public PraseXmlAction() { } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("定时任务执行中......"); } /** * 获取当前时间为文件夹名称 * * @return */ protected static String getFileName() { String fileNames = null; Date date = new Date(); SimpleDateFormat formatDateFormat = new SimpleDateFormat("yyyy-MM-dd"); fileNames = formatDateFormat.format(date); return fileNames; } /** * JDom解析xml文件 */ public void parseXml() { try { List<Element> elementList = getElementList(); for (Element element : elementList) { Element nameElement = element.getChild("举报信息反馈"); List<Element> children = nameElement.getChildren(); for (Element element2 : children) { Element nameElement2 = element2.getChild("举报编号"); if (nameElement2 != null) { System.out.println(" " + nameElement2.getName() + ":" + nameElement2.getTextTrim()); } Element valueElement = element2.getChild("状态"); if (valueElement != null) { System.out.println(" " + valueElement.getName() + ":" + valueElement.getTextTrim()); } Element descriptElement = element2.getChild("答复意见"); if (descriptElement != null) { System.out.println(" " + descriptElement.getName() + ":" + descriptElement.getTextTrim()); } System.out.println("--------------------"); } } } catch (Exception e) { System.out.println(e.getMessage()); } } public static List<Element> getElementList() throws FileNotFoundException, JDOMException, IOException { // 创建SAX建造者对象,该类构造方法的重载boolean类型的方法中validate表示是否验证xml文档 SAXBuilder saxBuilder = new SAXBuilder(false); InputStream inputStream = new FileInputStream(new File(xmlPath)); // 解析xml文档,返回document文档对象 Document document = saxBuilder.build(inputStream); // 获取根节点 Element rootElement = document.getRootElement(); // 获取根节点下的第一个子节点 List<Element> elementList = rootElement.getChildren(); return elementList; } }