您的位置:首页 > 编程语言 > Java开发

Spring的定时任务Timer+log4j的使用

2014-07-07 14:29 344 查看

1.构建spring项目

参考上一章节使用Maven构建Spring项目,这里我们不做详细说明,我们这一章节的内容是在上一章节Spring项目构建成功的基础上讲解的。

2.修改pom.xml,添加log4j的依赖

这里可以参加Spring的官方文档(http://docs.spring.io/spring/docs/4.0.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/)Using
Log4J这一部分。这里贴出pom.xml全部的代码。
<span style="font-family:Microsoft YaHei;font-size:12px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.carson.demo</groupId>
<artifactId>spring3</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring3 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!-- 添加Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<!-- 添加Spring依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.9.RELEASE</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>

</dependencies>
<build>
<finalName>spring3</finalName>
<outputDirectory>${basedir}/src/main/webapp/WEB-INF/classes</outputDirectory>
</build>
</project><span style="font-family:Microsoft YaHei;">
</span></span>

3.新建Log4J的配置文件,log4j.xml或者log4j.properties

这里参考Spring的官方文档,以log4j.properties为例。在src/main/resources目录下新建log4j.properties,代码如下:
<span style="font-family:Microsoft YaHei;font-size:12px;">log4j.rootCategory=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG</span>


4.新增Spring配置文件applicationContext.xml

applicationContext.xml配置文件可以参考使用Maven构建Spring项目这节,直接拷贝过来,然后添加注解定时任务的配置及说明。applicationContext.xml代码如下:
<span style="font-family:Microsoft YaHei;font-size:12px;"><?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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- 方法一:使用xml -->
<!-- <bean id="studentService" class="com.carson.spring.service.StudentServiceImpl"></bean> -->

<!-- 方法二:使用注解,告诉spring容器到base-package路径下去扫描所有的类,从而找到被注解的类。-->
<context:component-scan base-package="com.carson.spring.service"/>

</beans> </span>
要配置定时任务,需要在applicationContext.xml xmlns中添加 xmlns:task="http://www.springframework.org/schema/task",
在xsi:schemaLocation添加http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 然后加入注解的扫描路径<context:component-scan base-package="com.carson.spring.job" />
这里我在src/main/resources目录下新增了一个applicationContext-timer.xml专门用来配置定时任务。代码如下:
<span style="font-family:Microsoft YaHei;font-size:12px;"><?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:context="http://www.springframework.org/schema/context"
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/fex http://www.springframework.org/schema/fex/spring-fex-1.5.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<!--加入此段配置-->
<context:component-scan base-package="com.carson.spring.job" />
<!-- Enables the Spring Task @Scheduled programming model -->
<task:executor id="executor" pool-size="1" />
<task:scheduler id="scheduler" pool-size="1" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>  </span>


4.web.xml中配置Spring及log4j

<span style="font-family:Microsoft YaHei;font-size:12px;"><!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--contextConfigLocation在 ContextLoaderListener类中的默认值是 /WEB-INF/applicationContext.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/applicationContext.xml</param-value> -->
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<!-- log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>

</web-app>
</span>


5.测试

在src/main/java目录下新建一个定时任务类Timer.java,代码如下:
<span style="font-family:Microsoft YaHei;font-size:12px;">package com.carson.spring.job;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class Timer {

private Logger log = Logger.getLogger(this.getClass());

@Scheduled(fixedRate=5000)
public void clientInfoTimer() {
log.info("------------log4j------------");
}
}
</span>


把项目部署到tomcat,启动之后,控制台及日志文件中记录-----log4j------,至此项目构建完毕
整个项目目录结构如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: