从头认识Spring-1.8 怎样利用AOP记录日志?

本文通过用户登录的例子,详细介绍了如何使用AOP(面向切面编程)进行日志记录,包括实现步骤、关键代码及配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于之前有网友提出Aop的使用的问题,我们这一章节来讨论一下利用AOP记录日志。

我们下面将以用户登录为例子

1.domain

User类:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;

public class User {

	private String name = "";

	private int id = 0;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

 

用户这里我们比较简单,只记录id和name


登录实现类:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;

public class LoginServiceImpl {

	public void login(User user) {
		if (user != null) {
			System.out.println(user.getName() + " login");
		}
	}
}


登录实现类主要打印一下登录的信息。

 

日志类:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;

import org.aspectj.lang.JoinPoint;

public class Log {

	public void before() {
		System.out.println("before login);
	}

	public void after(JoinPoint joinpoint) {
		Object[] objects = joinpoint.getArgs();
		for (int i = 0; i < objects.length; i++) {
			User user = (User) objects[i];
			System.out.println("save login log:" + user.getName() + " login");
		}
	}
}


日志这里我们需要记录两个东西,一个在登录这个动作之前做的,一个是登录日志之后记录登录日志的操作,这里需要有一个注意的地方:

就是这里面的方法,可以带参数或者无参

而我们的数据流将可以通过JoinPoint这个类来获得

 

2.测试类:

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_8/ApplicationContext-test.xml" })
public class LoginTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testLogin() {
		LoginServiceImpl loginServiceImpl = applicationContext.getBean(LoginServiceImpl.class);
		User user = applicationContext.getBean(User.class);
		loginServiceImpl.login(user);
	}
}


只是简单的get那个Bean出来,然后登录一下。

 

3.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="user"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.User">
		<property name="id" value="1" />
		<property name="name" value="jack" />
	</bean>
	<bean id="loginServiceImpl"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.LoginServiceImpl" />
	<bean id="log"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.Log" />

	<aop:config>
		<aop:aspect ref="log">
			<aop:pointcut
				expression="execution(* com.raylee.my_new_spring.my_new_spring.ch01.topic_1_8.LoginServiceImpl.*(..))"
				id="logPointcut" />
			<aop:before method="before" pointcut-ref="logPointcut" />
			<aop:after method="after" pointcut-ref="logPointcut" />
		</aop:aspect>
	</aop:config>


</beans>


配置文件跟之前的aop那一章节的基本一样。

 

测试输出:

before login
jack login
save login log:jack login

 

总结:这一章节主要介绍怎样利用AOP记录日志,以及里面需要注意的JoinPoint。

 


目录:http://blog.youkuaiyun.com/raylee2007/article/details/50611627 

 

我的github:https://github.com/raylee2015/my_new_spring


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值