一、导包
上一篇博客(整合ssm),lib里就有此包
二、log4j 配置文件
#log4j.rootLogger=debug, stdout
#
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d %l %m %n
log4j.rootLogger=debug,logfile
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=C:\\Users\\LUO\\LOG\\mylog.txt
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%p %d %F %M %m%n
三、调用log4j中的记录日志方法
package com.java.exception;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalException {
//一般会定义成静态,减少它加载的次数
private static Logger logger = Logger.getLogger(GlobalException.class);
@ExceptionHandler(Exception.class)
public String myException(Exception e) {
logger.debug(e.getMessage());
return "error.jsp";
}
}
四、增加扫描范围
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 自動扫描Spring的注解 -->
<!--
特别要注意:base-package="com.java.controller"> 一定不能配置为com.java,这个这样配置了,事务将不起作用,
因为配置为com.java,将在SpringMvc中扫描@service,这样Spring的容器中将会有service的实例,而applicationContext.xml的扫描
就不会产生service的实例,可以事务的AOP将无法扩展功能。同时注意:controller中注入service实例只能用接口,不然就就会报错。
-->
<context:component-scan base-package="com.java.control,com.java.exception"></context:component-scan>
<!-- 扫描springmvc中的注解 -->
<!-- <mvc:annotation-driven></mvc:annotation-driven> -->
<!-- 数据验证 -->
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
</bean>
<!-- 上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="1048576"></property>
</bean>
<!--
<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
-->
</beans>
自己写了个错误,然后运行看结果