使用 Spring AOP 审计日志
我们正在研究 Spring 的 AOP 框架,用于在基于 Spring MVC 的应用程序中添加审核日志功能。
在我们的应用程序中,每当我们调用服务方法时,我们都会传递额外的审核日志对象(除了正常的方法参数之外)。该审核日志对象具有一些预先填充的属性,例如用户名、用户 ID 和用户 IP 地址等。
在实际的服务方法中,我们根据调用的方法或服务方法中执行的操作在审核日志对象上设置更多属性。
这是非常动态的,并且因方法而异。
在AOP类中我们可以拦截方法参数和返回值。但是,根据服务方法中执行的操作计算并然后在审核日志对象上设置的值又如何呢?该值在 AOP 类中不可用。
基本上,我们需要填充审核日志对象,然后在执行方法后将其保存在数据库中,并且仅在被拦截的方法中设置参数。
我们有什么办法可以做到这一点吗?
请帮忙。
We are looking at spring's AOP framework for adding Audit log functionality in our application based on Spring MVC.
In our application whenever we call a service method we pass extra audit log object (in addition to the normal method parameter). This audit log object has few properties pre populated like username, user id and user ip address etc.
In the actual service method we set few more properties on the audit log object depending on method being called or the operation being performed in the service method.
This is very dynamic and varies from method to method.
In the AOP classes we can intercept the method parameters and the return values. But what about the values which are being calculated based on the operation being performed in the service method and then set on the audit log object. This values wont be available in the AOP classes.
Basically we need to populate the audit log object and then save it in the db after execution of the method with the come parameters being set in method being intercepted only.
Is there any we can do this?
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 AOP 类中,您应该有权访问审计对象,因为您有权访问方法参数。所以方法返回后,在方法参数中找到审计对象并对其进行操作。审计对象是哪个参数并不重要,只要您可以按类型找到它即可。
您在审计对象上设置的值应该仍然存在,因为审计对象是通过引用传递的。
In the AOP class you should have access to the audit object since you have access to the method parameters. So after the method returns, find the audit object in the method parameters and operate on it. It doesn't matter which argument the audit object is, as long as you can find it by type.
The values you set on the audit object should still be there since the audit object was passed by reference.