Java for Web学习笔记(五三):Spring框架简介(2)一些准备

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相关文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值