SPRING:定时器

SPRING:定时器

项目结构
SPRING:定时器_第1张图片

applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <description>Spring公共配置文件description>
    <import resource="classpath:config/spring/applicationContext_service.xml"/>
    <import resource="classpath:config/spring/applicationContext_quartz.xml"/>

beans>

applicationContext_service.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <description>添加spring的注解配置,省去大部分xml来配置和注入beandescription>
    <context:component-scan base-package="spring.quartz.job" />

beans>

applicationContext_quartz.xml


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <description>quartz配置文件description>
    

    
    

    
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="true">
        
        <property name="triggers">
            <list>
                <ref bean="sendBirthSmsTaskTrigger"/>
            list>
        property>
    bean>

    
    <bean id="sendBirthSmsTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        
        <property name="jobDetail">
            <ref bean="sendBirthSms" />
        property>
        
        <property name="cronExpression">
            <value>*/1 * * * * ?value>
        property>
    bean>
     
    <bean id="sendBirthSms" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        
        <property name="targetObject" ref="sendSmsBirth" />
        
        <property name="targetMethod" value="executeTask" />
        
        <property name="concurrent" value="false" />
    bean>

beans>

SendSmsBirthJob.java

package spring.quartz.job;

import org.springframework.stereotype.Component;

@Component(value = "sendSmsBirth")
public class SendSmsBirthJob {

    public void executeTask() {
        System.out.println("SendSmsBirthJob.executeTask()");
    }
}

SendThreadServer.java

package spring;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SendThreadServer {

    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext(
                "classpath:config/spring/applicationContext.xml");
    }
}

运行结果

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/gitworkspaces/springQuartzTest/lib/slf4j-log4j12-1.7.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/gitworkspaces/springQuartzTest/lib/slf4j-simple-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:31 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:32 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:33 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:34 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:35 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:36 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:37 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:38 CST 2017
SendSmsBirthJob.executeTask(): Thu Jul 13 15:14:39 CST 2017

参考:http://blog.sina.com.cn/s/blog_5f9beca40101cmfq.html

你可能感兴趣的:(springmvc)