1.構建spring項目
2.修改pom.xml,添加log4j的依賴
<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
<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",<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.測試
<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------,至此項目構建完畢