java Spring项目中AOP最常见的用途是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4708776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the most common use for AOP in spring project
提问by Mat B.
After reviewing the AOP pattern, I'm overwhelmed with the ways of how and what to use it for in my spring project.
在回顾了 AOP 模式之后,我对在我的 spring 项目中如何使用它以及如何使用它的方式感到不知所措。
I'd like to use it as audit log system of all the financial business logic. It just seems to be easy to integrate. But I'd like to hear your take on this.
我想用它作为所有金融业务逻辑的审计日志系统。它似乎很容易集成。但我想听听你对此的看法。
The question is - what other uses should I consider that are common for this pattern? I would not mind refactoring my current logic to be used with AOP as long as there is benefits to it.
问题是 - 我应该考虑这种模式常见的其他用途是什么?只要有好处,我不介意重构我当前的逻辑以与 AOP 一起使用。
采纳答案by Nilesh
The most common usage is where your application has cross cutting concerns i.e. a piece of logic or code that is going to be written in multiple classes/layers.
最常见的用法是您的应用程序具有横切关注点,即将要在多个类/层中编写的一段逻辑或代码。
And this could vary based on your needs. Some very common examples of these could be:
这可能会根据您的需要而有所不同。其中一些非常常见的例子可能是:
- Transaction Management
- Logging
- Exception Handling (especially when you may want to have detailed traces or have some plan of recovering from exceptions)
- Security aspects
- Instrumentation
- 交易管理
- 日志记录
- 异常处理(尤其是当您可能想要详细的跟踪或有一些从异常中恢复的计划时)
- 安全方面
- 仪表
Hope that helps.
希望有帮助。
回答by Axel Fontaine
The most common use is probably the declarative transaction handling using @Transactional
.
最常见的用途可能是使用@Transactional
.
回答by CoolBeans
Besides logging/auditing and declarative transaction handling as mentioned by Axel, I would say another usage of AOP is as a request interceptor. For example, let's say you need all requests coming of a server to be intercepted so that you can do something with it (may be to keep track of which app is sending what request to what other app or what database, etc).
除了 Axel 提到的日志记录/审计和声明式事务处理之外,我想说 AOP 的另一种用法是作为请求拦截器。例如,假设您需要拦截来自服务器的所有请求,以便您可以对它做一些事情(可能是跟踪哪个应用程序正在向其他应用程序发送什么请求或什么数据库等)。
回答by Amit Patil
It can be used to expose custom metrics (Instrumentation of service) for Alerting and Monitoring of service using client libraries like dropwizard, prometheus.
它可用于使用 dropwizard、prometheus 等客户端库公开用于警报和服务监控的自定义指标(服务检测)。
It helped us, to
它帮助我们,
- Keep this instrumentation code (Not a business logic) outside of actual business logic
Keep these cross-cutting concerns at one single place.
Declaratively apply them wherever required.
- 将此检测代码(不是业务逻辑)保留在实际业务逻辑之外
将这些跨领域的关注点放在一个地方。
在需要的地方声明性地应用它们。
For example, To expose
例如,要暴露
- Total bytes returned by REST AIP - (Can be done in after advice)
- Total time taken by REST API i.e server-in and server-out rime- (Can be done using around advice).
- REST AIP 返回的总字节数 -(可以在 after 建议中完成)
- REST API 所用的总时间,即服务器输入和服务器输出时间(可以使用 around 建议来完成)。
回答by RicoZ
You can use AOP for your security concerns, for example allow/disallow method access. Another usage of aop is to test your application performance.
您可以将 AOP 用于您的安全问题,例如允许/禁止方法访问。aop 的另一个用途是测试您的应用程序性能。
回答by AngerClown
Using AOP for audit logging is a perfectly valid use of AOP. You can turn it off for testing and change it as requirements change in production.
使用 AOP 进行审计日志记录是对 AOP 的完全有效的使用。您可以将其关闭以进行测试并随着生产中的需求变化而更改它。
The only downside in this case is if you were planning on doing the audit log via SQL. It may be more performant to implement this kind of auditing as triggers directly in the DB.
在这种情况下唯一的缺点是如果您计划通过 SQL 执行审计日志。将这种审计作为触发器直接在数据库中实现可能会更高效。
回答by Chris Thompson
As an answer slightly different from what @Axel said, using it to automatically intercept all of your data access calls and apply transactions appropriately is phenomenal. I have mine set up to implement all calls to my dao package that don't start with "get" in a transaction and then anything performed in a method starting with "get" is treated as read only. It's fantastic because aside from the initial setup, I don't have to worry about it, just follow the naming convention.
作为与@Axel 所说的略有不同的答案,使用它来自动拦截所有数据访问调用并适当地应用事务是惊人的。我已经设置为实现对我的 dao 包的所有调用,这些调用在事务中不以“get”开头,然后在以“get”开头的方法中执行的任何操作都被视为只读。这太棒了,因为除了初始设置之外,我不必担心它,只需遵循命名约定即可。