初识 spring整合定时任务Quartz+mybatis
1.spring-quartz.xml
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd" >
<context:component-scan base-package="com.ylz.quartz" />
<!-- 定时任务 -->
<bean id="myQuartzJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<property name="jobClass">
<!-- 上面的任务代理类 -->
<value>com.ylz.quartz.bean.DetailQuartzJobBean</value>
</property>
<property name="jobDataAsMap">
<map>
<!-- 实际的任务的Bean name,填上EventMonitorService的Bean name -->
<entry key="targetObject" value="MyQuartzJob" />
<!-- 执行Bean中的哪个方法 -->
<entry key="targetMethod" value="execute" />
</map>
</property>
<property name="durability" value="true"></property>
</bean>
<!-- 定时任务Trigger -->
<bean id="myQuartzJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<!-- 任务代理Bean name -->
<ref bean="myQuartzJob" />
</property>
<property name="cronExpression">
<!-- 配置表达式,这里表示凌晨2点30分分钟执行 -->
<value>0 * * * * ?</value>
</property>
</bean>
<!-- 任务调度入口 -->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 任务列表,可以配置多个任务加入到该List -->
<property name="triggers">
<list>
<ref bean="myQuartzJobTrigger"/>
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties" />
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
<property name="startupDelay" value="30" />
<property name="autoStartup" value="true" />
<property name="overwriteExistingJobs" value="true" />
</bean>
</beans>
2.spring-mybatis
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="url" value="${db.url}"/>
<property name="driverClassName" value="${db.driverClassName}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="jmxEnabled" value="${db.jmxEnabled}" />
<property name="testWhileIdle" value="${db.testWhileIdle}"/>
<property name="testOnBorrow" value="${db.testOnBorrow}" />
<property name="validationQuery" value="${db.validationQuery}" />
<property name="testOnReturn" value="${db.testOnReturn}"/>
<property name="validationInterval" value="${db.validationInterval}"/>
<property name="timeBetweenEvictionRunsMillis" value="${db.timeBetweenEvictionRunsMillis}"/>
<property name="maxActive" value="${db.maxActive}"/>
<property name="initialSize" value="${db.initialSize}"/>
<property name="maxWait" value="${db.maxWait}"/>
<property name="removeAbandonedTimeout" value="${db.removeAbandonedTimeout}"/>
<property name="minEvictableIdleTimeMillis" value="${db.minEvictableIdleTimeMillis}"/>
<property name="minIdle" value="${db.minIdle}"/>
<property name="maxIdle" value="${db.maxIdle}" />
<property name="logAbandoned" value="${db.logAbandoned}"/>
<property name="removeAbandoned" value="${db.removeAbandoned}"/>
<property name="jdbcInterceptors" value="${db.jdbcInterceptors}"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置mybatis打印日志 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 自动扫描mapping.xml文件 -->
<!-- <property name="mapperLocations" value="classpath:com/ylz/quartz/dao/mapping/*.xml"></property> -->
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.etoc.account.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
2.项目上传git
https://github.com/yunfanYlz/spring-quartz
3.本次搭建项目遇到问题
在window上运行正常,打包放到Linux上启动报错
解决方案:
vi 编辑 /etc/hosts文件,将当前服务器主机名加入
hostname 查看当前服务器主机名