项目中会经常用到定时器,因此,其quartz的使用,我们必须要掌握。下面就以例子来讲解如何在spring中整合quartz,

使用注解配置的方式来实现定时执行任务。

一、引入jar包

    项目中需引入quartz的jar包,由于整合到spring中,肯定也引入了spring的相关jar包。

    例子中引用的是quartz-2.1.1版本,使用maven管理项目,pom文件需引入quartz依赖

  spring注解配置quartz应用

二、spring配置文件中配置 (applicationContext.xml)

  1)      xmlns和 xsi:schemaLocation配置

        xmlns:task="http://www.springframework.org/schema/task"

        xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd "

    

 2)task任务扫描注解   

  <!-- 加载定时任务 -->
  <task:annotation-driven/>

 3)扫描的位置

       <!-- 自动扫描 -->

      <context:component-scan base-package="com.hik.quartz"/>

4)写任务实现类及配置任务定时执行方式

    spring注解配置quartz应用

 

使用spring注解配置quartz,非常方便实用。

下面给出配置spring配置文件配置

 1 <?xml version="1.0" encoding="UTF-8"?>    
 2 <beans xmlns="http://www.springframework.org/schema/beans"    
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 4     xmlns:p="http://www.springframework.org/schema/p"  
 5     xmlns:aop="http://www.springframework.org/schema/aop"   
 6     xmlns:context="http://www.springframework.org/schema/context"  
 7     xmlns:jee="http://www.springframework.org/schema/jee"  
 8     xmlns:tx="http://www.springframework.org/schema/tx"  
 9     xmlns:task="http://www.springframework.org/schema/task"
10     xsi:schemaLocation="    
11         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
12         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
13         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
14         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
15         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
16         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd ">    
17         
18     <!-- 自动扫描 -->
19     <context:component-scan base-package="com.hik.service" />
20     <context:component-scan base-package="com.hik.quartz"/>
21     
22     <!-- 加载定时任务 -->
23     <task:annotation-driven/>
24     
25     <!-- 配置数据源 -->
26     <bean id="dataSource"
27         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
28         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
29         <property name="url" value="jdbc:mysql://localhost:3306/db_crm"/>
30         <property name="username" value="root"/>
31         <property name="password" value="passwd"/>
32     </bean>
33 
34     <!-- 配置mybatis的sqlSessionFactory -->
35     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
36         <property name="dataSource" ref="dataSource" />
37         <!-- 自动扫描mappers.xml文件 -->
38         <property name="mapperLocations" value="classpath:com/hik/mappers/*.xml"></property>
39         <!-- mybatis配置文件 -->
40         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
41     </bean>
42 
43     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
44     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
45         <property name="basePackage" value="com.hik.dao" />
46         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
47     </bean>
48 
49     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
50     <bean id="transactionManager"
51         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
52         <property name="dataSource" ref="dataSource" />
53     </bean>
54     
55     <!-- 配置事务通知属性 -->  
56     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
57         <!-- 定义事务传播属性 -->  
58         <tx:attributes>  
59             <tx:method name="insert*" propagation="REQUIRED" />  
60             <tx:method name="update*" propagation="REQUIRED" />  
61             <tx:method name="edit*" propagation="REQUIRED" />  
62             <tx:method name="save*" propagation="REQUIRED" />  
63             <tx:method name="add*" propagation="REQUIRED" />  
64             <tx:method name="new*" propagation="REQUIRED" />  
65             <tx:method name="set*" propagation="REQUIRED" />  
66             <tx:method name="remove*" propagation="REQUIRED" />  
67             <tx:method name="delete*" propagation="REQUIRED" />  
68             <tx:method name="change*" propagation="REQUIRED" />  
69             <tx:method name="check*" propagation="REQUIRED" />  
70             <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
71             <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
72             <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
73             <tx:method name="*" propagation="REQUIRED" read-only="true" />  
74         </tx:attributes>  
75     </tx:advice>  
76   
77     <!-- 配置事务切面 -->  
78     <aop:config>  
79         <aop:pointcut id="serviceOperation"  
80             expression="execution(* com.hik.service.*.*(..))" />  
81         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
82     </aop:config>  
83     
84    
85     
86 </beans>
View Code

相关文章:

  • 2021-11-29
  • 2021-05-11
  • 2021-12-31
  • 2021-11-25
猜你喜欢
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
相关资源
相似解决方案