准备工作 下载quartz和spring相应的包
 
1.编写测试类
quartz结合spring例子_springpublic class SimpleService implements Serializable {        
quartz结合spring例子_spring         private static final long serialVersionUID = 122323233244334343L;
quartz结合spring例子_spring        private static final Log logger = LogFactory.getLog(SimpleService.class);
quartz结合spring例子_spring        public void testMethod1(){
quartz结合spring例子_spring                //这里执行定时调度业务 便于显示明细添加些特殊符号
quartz结合spring例子_spring                logger.info("testMethod1...@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@....1");
quartz结合spring例子_spring        }
quartz结合spring例子_spring        public void testMethod2(){
quartz结合spring例子_spring                logger.info("testMethod2....###################################################!!!!!!!!!!!!!!!!!!!!...2");
quartz结合spring例子_spring        }
quartz结合spring例子_spring}        
2. 开始配置spring。
(1)配置spring的数据源
quartz结合spring例子_spring
quartz结合spring例子_spring        <bean id="dataSource"
quartz结合spring例子_spring                class="org.apache.commons.dbcp.BasicDataSource">
quartz结合spring例子_spring                <property name="driverClassName">
quartz结合spring例子_spring                        <value>com.mysql.jdbc.Driver</value>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring                <property name="url">
quartz结合spring例子_spring                        <value>jdbc:mysql://127.0.0.1:3306/going</value>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring                <property name="username">
quartz结合spring例子_spring                        <value>root</value>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring                <property name="password">
quartz结合spring例子_spring                        <value>root</value>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring                <property name="maxActive">
quartz结合spring例子_spring                        <value>100</value>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring                <property name="maxIdle">
quartz结合spring例子_spring                        <value>2</value>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring                <property name="maxWait">
quartz结合spring例子_spring                        <value>1200</value>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring        </bean>
<!-- 下面是tomcat的 数据源 jndi的配置-->
     
quartz结合spring例子_spring            <!--
quartz结合spring例子_spring         <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" abstract="false">
quartz结合spring例子_spring                <property name="jndiName">
quartz结合spring例子_spring         <value>java:comp/env/jdbc/mysqlds</value>
quartz结合spring例子_spring </property>
quartz结合spring例子_spring        </bean>
(2)配置spring和job的结合

  
quartz结合spring例子_spring    <bean id="simpleService" class="com.going.oa.quartz.example5.SimpleService">    
quartz结合spring例子_spring</bean>
quartz结合spring例子_spring        
quartz结合spring例子_spring        <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
quartz结合spring例子_spring                <property name="dataSource">
quartz结合spring例子_spring                        <ref bean="dataSource"/>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring                <property name="applicationContextSchedulerContextKey" value="applicationContextKey"/>
quartz结合spring例子_spring                <property name="configLocation" value="classpath:quartz_priority.properties"/>
quartz结合spring例子_spring                <property name="triggers">
quartz结合spring例子_spring                <list>
quartz结合spring例子_spring                <ref bean="trigger1"/>
quartz结合spring例子_spring                <ref bean="trigger2"/>
quartz结合spring例子_spring                </list>
quartz结合spring例子_spring                </property>
quartz结合spring例子_spring        </bean>
quartz结合spring例子_spring        <bean id="jobDetail1"    class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    
quartz结合spring例子_spring                <property name="targetObject" ref="simpleService"/>
quartz结合spring例子_spring                <property name="targetMethod" value="testMethod1"/>
quartz结合spring例子_spring                <property name="concurrent" value="false" />
quartz结合spring例子_spring        </bean>
quartz结合spring例子_spring        
quartz结合spring例子_spring                <bean id="trigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
quartz结合spring例子_spring                <property name="jobDetail" ref="jobDetail1"/>
quartz结合spring例子_spring                <property name="cronExpression" value="0/5 * * ? * * *"/>
quartz结合spring例子_spring        </bean>
quartz结合spring例子_spring        <bean id="jobDetail2"    class="frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    
quartz结合spring例子_spring                <property name="targetObject" ref="simpleService"/>
quartz结合spring例子_spring                <property name="targetMethod" value="testMethod2"/>
quartz结合spring例子_spring                <property name="concurrent" value="false" />
quartz结合spring例子_spring        </bean>
quartz结合spring例子_spring        
quartz结合spring例子_spring        <bean id="trigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
quartz结合spring例子_spring                <property name="jobDetail" ref="jobDetail2"/>
quartz结合spring例子_spring                <property name="startDelay" value="1"/>
quartz结合spring例子_spring                <property name="repeatCount" value="100"/>
quartz结合spring例子_spring                <property name="repeatInterval" value="1000"/>
quartz结合spring例子_spring        </bean>
quartz结合spring例子_spring</beans>
注意上面frameworkx.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean类在spring的jar包中不提供,spring提供的MethodInvokingJobDetailFactoryBean有个bug所以需要到网上去下载这俩个文件http://jira.springframework.org/browse/SPR-3797
 
3编写测试类
quartz结合spring例子_spring
quartz结合spring例子_springpackage com.going.oa.quartz.example5;
quartz结合spring例子_springimport org.springframework.context.ApplicationContext;
quartz结合spring例子_springimport org.springframework.context.support.ClassPathXmlApplicationContext;
quartz结合spring例子_spring/**
quartz结合spring例子_spring * Created by IntelliJ IDEA.
quartz结合spring例子_spring * User: weiyong
quartz结合spring例子_spring * Date: 2010-3-23
quartz结合spring例子_spring * Time: 13:57:51
quartz结合spring例子_spring * To change this template use File | Settings | File Templates.
quartz结合spring例子_spring */

quartz结合spring例子_springpublic class MainTest {    
quartz结合spring例子_spring            /**
quartz结合spring例子_spring             * @param args
quartz结合spring例子_spring             */

quartz结合spring例子_spring            public static void main(String[] args) {
quartz结合spring例子_spring                    ApplicationContext springContext = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext-resources.xml","classpath:applicationContext-quartz.xml"});
quartz结合spring例子_spring            }
quartz结合spring例子_spring    }
quartz结合spring例子_spring
quartz结合spring例子_spring
运行Ok