Spring的定時任務Timer+log4j的使用

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------,至此項目構建完畢
整個項目目錄結構如下:




發佈了50 篇原創文章 · 獲贊 48 · 訪問量 45萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章