前提:已经部署spring(接上篇:spring 注解 容器的注入注出)
一:新增依赖包
1:log4j-1.2.9.jar(日志包)
2:spring-aspects-4.1.6.RELEASE.jar(spring包中存在)
3:aopalliance-1.0.jar(不在spring包中)
4:aspectjweaver-1.7.3.jar(不在spring包中)
二:新增切面类(aop.Log4jHandlerAOP)
三:新增log4j.properties(放入scr目录下)
四:修改applicationContext.xml
1:beans属性新增:xmlns:aop="http://www.springframework.org/schema/aop"
2:beans属性(xsi:schemaLocation)新增:http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
3:配置“切面”与“切入点”
ps:
1:applicationContext.xml
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config /> <!-- 根据注解扫描类放入容器 --> <context:component-scan base-package="dao,service,servlet" /> <!--升级: 注解,自动注入 --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <bean id="log4jHandlerAOP" class="aop.Log4jHandlerAOP"></bean> <aop:config> <aop:aspect id="logAspect" ref="log4jHandlerAOP"> <aop:pointcut id="logPointCut" expression="execution(* service.*.* (..))" /> <aop:around method="RecordLog" pointcut-ref="logPointCut" /> </aop:aspect> </aop:config> </beans>
2:Log4jHandlerAOP.java
package aop; import org.apache.log4j.Logger; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; @Component("log4jHandlerAOP") public class Log4jHandlerAOP { Logger logger; public Object RecordLog(ProceedingJoinPoint pjp) throws Throwable { logger = Logger.getLogger(Log4jHandlerAOP.class); String className = pjp.getTarget().getClass().getSimpleName(); String methodName = pjp.getSignature().getName(); String userName = "豪1817"; StringBuffer sb = new StringBuffer(); sb.append("userName: " + userName); sb.append(" className: " + className); sb.append(" methodName: " + methodName); logger.info(sb.toString()); Object obj = pjp.proceed(); return obj; } }
3:log4j.properties
### set log levels ### log4j.rootLogger = debug,stdout,D ,E ### 输出到控制台 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n ### 输出到日志文件 按天创建日志 ### log4j.appender.D = org.apache.log4j.DailyRollingFileAppender log4j.appender.D.File = C:/D/log/logs/logPerDay.log log4j.appender.D.Append = true ## 输出DEBUG级别以上的日志 log4j.appender.D.Threshold = DEBUG log4j.appender.D.layout = org.apache.log4j.PatternLayout log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 输出到日志文件 按大小创建日志 ### log4j.appender.E=org.apache.log4j.RollingFileAppender log4j.appender.E.File=C:/D/log/logs/log.log log4j.appender.E.MaxFileSize=50KB log4j.appender.E.MaxBackupIndex=5 log4j.appender.E.layout=org.apache.log4j.PatternLayout log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
时间: 2024-10-11 19:35:07