ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
下面实现在执行自己的业务代码`StudentServiceImpl.add`方法之前,先插入一个处理日志的方法`printLogger`。 <br/> 步骤如下: **1. 创建一个Maven项目** ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.9</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.9.RELEASE</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>compile</scope> </dependency> </dependencies> ``` **2. 创建接口以及实现该接口的类** ```Java public interface StudentService { void add (int x, int y); } ``` ```java public class StudentServiceImpl implements StudentService { /** * 目标函数 */ @Override public void add(int x, int y) { System.out.println("------目标函数的输出------\n" + (x + y)); } } ``` **3. 定义一个切面** ```java public class AspectLogger { private static final Logger logger = Logger.getLogger(AspectLogger.class); /** * 增强函数,在同一个切面中如果存在多个重载的增强函数,即使参数列表不一致 * 默认起作用的始终是第一个增强函数,其它的无效 */ public void printLogger(JoinPoint jPoint) { logger.info("\n------增强函数的输出------" + "\n增强方式:前置增强" + "\n目标对象:" + jPoint.getTarget() + "\n目标函数:" + jPoint.getSignature().getName() + "\n目标函数的参数:" + Arrays.toString(jPoint.getArgs())); } } ``` **4. 将相关组件注入到IoC容器中** *`resources/ApplicationContext.xml`* ```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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 目标对象注入 --> <bean id="studentService" class="com.learn.spring.aop.service.impl.StudentServiceImpl"/> <!-- 切面对象注入 --> <bean id="aspectLogger" class=" com.learn.spring.aop.aspect.AspectLogger"/> <!-- 切面配置 --> <aop:config> <!-- 定义切入点 id: 切入点唯一标识 expression: 切入点表达式,只要任一目标函数的定义符合该表达式,就会被做增强处理 --> <aop:pointcut id="add" expression="execution(public void add(int, int))"/> <!-- 定义切面 ref: 上面切面对象的id值 --> <aop:aspect ref="aspectLogger"> <!-- 增强类型:前置增强 method: 增强方法,对应切面对象中的方法,即AspectLogger.printLogger方法 pointcut-ref: 引用上面定义的切入点 --> <aop:before method="printLogger" pointcut-ref="add"/> </aop:aspect> </aop:config> </beans> ``` **5. log4j日志配置文件`resources/log4j.properties`** ```java ############## log4j 日志相关配置 ############## log4j.rootLogger=DEBUG,CONSOLE,file #log4j.rootLogger=ERROR,ROLLING_FILE log4j.logger.cn.smbms.dao=debug log4j.logger.com.ibatis=debug log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug log4j.logger.java.sql.Connection=debug log4j.logger.java.sql.Statement=debug log4j.logger.java.sql.PreparedStatement=debug log4j.logger.java.sql.ResultSet=debug log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug ###################################################################################### # Console Appender 日志在控制输出配置 ###################################################################################### log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=error log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n ###################################################################################### # DailyRolling File 每天产生一个日志文件,文件名格式:log2009-09-11 ###################################################################################### log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.DatePattern=yyyy-MM-dd log4j.appender.file.File=log.log log4j.appender.file.Append=true log4j.appender.file.Threshold=error log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n log4j.logger.com.opensymphony.xwork2=error ``` **6. 测试** ```java @Test public void test01() { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); StudentService studentService = (StudentService) context.getBean("studentService"); studentService.add(10, 20); } ``` 输出结果如下,因为使用的是前置增强,所以增强的输出会在目标函数的输出前面。 ``` ------增强函数的输出------ 增强方式:前置增强 目标对象:com.learn.spring.aop.service.impl.StudentServiceImpl@294425a7 目标函数:add 目标函数的参数:[10, 20] ------目标函数的输出------ 30 ```