springMVC实现定时器
//springmvc.xml文件配置
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "> <mvc:annotation-driven /> <!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="weixin" /> <!-- 定时器扫描的包--> <context:component-scan base-package="timer"/> <!-- 启动定时器 --> <task:annotation-driven></task:annotation-driven> <!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 --> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 这个类用于Spring MVC视图解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=utf-8</value> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 --> </list> </property> </bean> <!-- 支持上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <!-- 100M --> <property name="maxUploadSize" value="10485760000"></property> <property name="defaultEncoding" value="utf-8"></property> </bean> <!-- 数据库配置--> <!-- 使用配置文件 --> <context:property-placeholder location="classpath:properties/mysql.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 连接池最大使用连接数 --> <property name="maxActive" value="20"/> <!-- 初始化连接大小 --> <property name="initialSize" value="1"/> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="60000"/> <!-- 连接池最小空闲 --> <property name="minIdle" value="3"/> <!-- 自动清除无用连接 --> <property name="removeAbandoned" value="true"/> <!-- 清除无用连接的等待时间 --> <property name="removeAbandonedTimeout" value="180"/> <!-- 连接属性 --> <property name="connectionProperties" value="clientEncoding=UTF-8"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--指定实体类映射文件,可以指定同时指定某一包以及子包下面的所有配置文件 --> <property name="mapperLocations" value="classpath:mapper/**/*.xml"/> <!--mybatis全局配置文件 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <!-- spring与mybatis整合配置,扫描所有dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zjn.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
//测试类
package timer; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import weixin.IWeChatService; @Component public class TestTask { @Autowired private IWeChatService service; private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Scheduled(cron = "*/5 * * * * ?") public void testTask(){ System.out.println("每隔一分钟时间打印时间"+FORMAT.format(new Date())); System.out.println("进入测试==================="); service.sendTextMsg("HongChenShaLou","12345","555","我今天很忙"); System.out.println("结束==================="); } }