maven的pom.xml
我们在pom.xml中增加下面内容,本次我们给出较为完整的依赖关系,可以作为模板:
<!-- 我们将包的版本号抽错来作为参数,如有可能尽可能使用较新的版本,可以在官方maven查询,采用这种方式,可以有统一的模板,只在这里改版本号即可。Spring相关的用粗体表示,但其他通常也配套使用 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson.version>2.9.0.pr2</jackson.version>
<log4j.version>2.8.1</log4j.version>
<websocket.version>1.1</websocket.version>
<commons.lang3.version>3.5</commons.lang3.version>
<inject.version>1</inject.version>
<annotation-api.version>1.3</annotation-api.version>
<spring.framework.version>4.3.7.RELEASE</spring.framework.version>
</properties>
<!-- 下面给出完整的依赖关系 -->
<dependencies>
<!-- PartI:Servlet和jsp的相关部分 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.2-b02</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.servlet.jsp.jstl</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<!-- PartII:项目常用的部分,包括websocket,common-langs,jackson -->
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>${websocket.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.lang3.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
<scope>compile</scope>
</dependency>
<!-- PartIII:log4j2部分,spring使用了Apache Commons Logging,如果要显示其log,就需要引入log4j-jcl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<!-- PartIV:Spring相关部分 -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>${inject.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${annotation-api.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.framework.version}</version>
</dependency>
</dependencies>
配置log4j2.xml
同样,我们希望给出一个通用的模板<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<properties>
<property name="logPath">D:\logs</property>
<property name="logName">chapter12</property>
</properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] [%-5level] %c{1}:%L - %msg%n"/>
</Console>
<Console name="SpringConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] [%-5level] (Spring) %c{1} - %msg%n"/>
</Console>
<RollingFile name="FileAppender" fileName="${logPath}/${logName}.log"
filePattern="../logs/${logName}-%d{yyyyMMdd}-%i.log.zip">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %X{username} [%-5level] %c{1}:%L: %msg%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy min="1" max="25" />
</RollingFile>
<RollingFile name="SpringFileAppender" fileName="${logPath}/${logName}.log"
filePattern="../logs/${logName}-%d{yyyyMMdd}-%i.log.zip">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %X{username} [%-5level] (Spring) %c{1}: %msg%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<DefaultRolloverStrategy min="1" max="25" />
</RollingFile>
</appenders>
<loggers>
<root level="warn">
<appender-ref ref="Console" />
<appender-ref ref="FileAppender" />
</root>
<!-- 本项目的log,等级为debug -->
<logger name="cn.wei.flowingflying.chapter" level="debug" />
<logger name="org.apache" level="info" />
<!-- Spring的log,等级为INFO -->
<logger name="org.springframework" level="info" additivity="false">
<appender-ref ref="SpringConsole" />
<appender-ref ref="SpringFileAppender" />
</logger>
</loggers>
</configuration>
相关链接: 我的Professional Java for Web Applications相关文章