- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在为以下问题寻找实用的解决方案:
我正在寻找的是有一个通用的 AOP 错误处理建议。它将包装每个方法的代码,这些方法是外部库中方法的直接覆盖或实现。基本上执行 try/catch 以进行错误恢复。
我当然可以手动搜索它们。然而,我们的代码库是广泛的。覆盖外部库类方法的可能性也是如此。因此考虑 AOP 来帮助我。
加载时编织被排除在外,因为它涉及到一个网络应用程序。可能无法使用应用程序服务器添加 JVM 代理程序。所以一定是编译时编织。
有人知道如何用 AspectJ 做到这一点吗?
最佳答案
假设您的外部库类/接口(interface)位于某个包中,例如 org.external.library
或其任何子包中,您可以用 AspectJ 语法将这些类/接口(interface)表示为 org.external.library..*+
. ..
表示“包含子包”,*
表示“所有类”,+
表示“包含子类”。现在附加 .*(..)
,您将获得所有方法,而不管这些类的名称和参数数量。
现在以切入点为例
execution(!static * org.external.library..*+.*(..))
无论返回类型如何,拦截任何库子类中的所有非静态方法。
坏消息是您不能将切入点限制为覆盖方法,因为 @Override
注释具有保留类型“source”,即它不可用于方面匹配。但也许您只想将切入点缩小到公共(public)方法,以便排除内部方法抛出的错误。这取决于您的情况以及您想如何处理它。假设非重写方法也可以破坏基础组件,不过切入点很好。
这是一个自洽的样本:
库类/接口(interface):
package org.external.library;
public abstract class AbstractBase {
public abstract String getText();
public abstract int getNumber();
public abstract void doSomething();
}
package org.external.library;
public interface Service {
void start();
void stop();
boolean isRunning();
}
不扩展任何库类的类:
如您所见,出于演示目的,此类在约 50% 的所有情况下随机抛出运行时异常。我们希望它们不由我们的方面处理。
package de.scrum_master.app;
import java.util.Random;
public class MyOwnClass {
private static final Random RANDOM = new Random();
public String getGreeting(String recipient) {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot get greeting for '" + recipient + "'");
return "Hello " + recipient + "!";
}
}
扩展/实现库类/接口(interface)的类:
如您所见,出于演示目的,这些类在大约 50% 的情况下还会随机抛出运行时异常。我们希望它们由我们的方面处理。
main
方法通过多次调用每个方法来演示整个设置,不处理重写库类中的任何错误,而只处理我们自己的类中的错误。
package de.scrum_master.app;
import java.util.Random;
import org.external.library.Service;
public class FooService implements Service {
private static final Random RANDOM = new Random();
private boolean isRunning;
@Override
public void start() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot start");
isRunning = true;
}
@Override
public void stop() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot stop");
isRunning = false;
}
@Override
public boolean isRunning() {
return isRunning;
}
public void pause() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot pause");
isRunning = false;
}
public void resume() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot resume");
isRunning = true;
}
}
package de.scrum_master.app;
import java.util.Random;
import org.external.library.AbstractBase;
public class Application extends AbstractBase {
private static final Random RANDOM = new Random();
@Override
public String getText() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot get text");
return "Hello world!";
}
@Override
public int getNumber() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot get number");
return RANDOM.nextInt(10);
}
@Override
public void doSomething() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot do something");
}
public void doSomethingElse() {
if (RANDOM.nextBoolean())
throw new RuntimeException("cannot do something else");
}
public static void main(String[] args) {
Application application = new Application();
FooService fooService = new FooService();
MyOwnClass myOwnClass = new MyOwnClass();
for (int i = 0; i < 5; i++) {
application.getText();
application.getNumber();
application.doSomething();
application.doSomethingElse();
fooService.start();
fooService.pause();
fooService.isRunning();
fooService.resume();
fooService.isRunning();
fooService.stop();
try {
myOwnClass.getGreeting("world");
myOwnClass.getGreeting("guys");
}
catch (Exception e) {
System.out.println("Uncaught by aspect: " + e);
}
}
}
}
错误处理方面:
package de.scrum_master.aspect;
public aspect ErrorHandler {
Object around() : execution(!static * org.external.library..*+.*(..)) {
try {
return proceed();
}
catch (Exception e) {
System.out.println(thisJoinPoint + " -> " + e);
return null;
}
}
}
控制台输出:
由于代码中的随机化,每次运行应用程序时输出看起来都有些不同:
execution(int de.scrum_master.app.Application.getNumber()) -> java.lang.RuntimeException: cannot get number
execution(void de.scrum_master.app.Application.doSomething()) -> java.lang.RuntimeException: cannot do something
execution(void de.scrum_master.app.Application.doSomethingElse()) -> java.lang.RuntimeException: cannot do something else
execution(void de.scrum_master.app.FooService.start()) -> java.lang.RuntimeException: cannot start
execution(void de.scrum_master.app.FooService.pause()) -> java.lang.RuntimeException: cannot pause
execution(void de.scrum_master.app.FooService.resume()) -> java.lang.RuntimeException: cannot resume
execution(void de.scrum_master.app.FooService.stop()) -> java.lang.RuntimeException: cannot stop
Uncaught by aspect: java.lang.RuntimeException: cannot get greeting for 'world'
execution(String de.scrum_master.app.Application.getText()) -> java.lang.RuntimeException: cannot get text
execution(int de.scrum_master.app.Application.getNumber()) -> java.lang.RuntimeException: cannot get number
execution(void de.scrum_master.app.Application.doSomething()) -> java.lang.RuntimeException: cannot do something
execution(void de.scrum_master.app.FooService.start()) -> java.lang.RuntimeException: cannot start
execution(void de.scrum_master.app.FooService.pause()) -> java.lang.RuntimeException: cannot pause
execution(void de.scrum_master.app.FooService.stop()) -> java.lang.RuntimeException: cannot stop
Uncaught by aspect: java.lang.RuntimeException: cannot get greeting for 'guys'
execution(void de.scrum_master.app.Application.doSomething()) -> java.lang.RuntimeException: cannot do something
execution(void de.scrum_master.app.FooService.start()) -> java.lang.RuntimeException: cannot start
execution(void de.scrum_master.app.FooService.pause()) -> java.lang.RuntimeException: cannot pause
Uncaught by aspect: java.lang.RuntimeException: cannot get greeting for 'guys'
execution(void de.scrum_master.app.Application.doSomethingElse()) -> java.lang.RuntimeException: cannot do something else
execution(void de.scrum_master.app.FooService.resume()) -> java.lang.RuntimeException: cannot resume
execution(void de.scrum_master.app.FooService.stop()) -> java.lang.RuntimeException: cannot stop
Uncaught by aspect: java.lang.RuntimeException: cannot get greeting for 'world'
execution(String de.scrum_master.app.Application.getText()) -> java.lang.RuntimeException: cannot get text
execution(int de.scrum_master.app.Application.getNumber()) -> java.lang.RuntimeException: cannot get number
execution(void de.scrum_master.app.Application.doSomething()) -> java.lang.RuntimeException: cannot do something
execution(void de.scrum_master.app.Application.doSomethingElse()) -> java.lang.RuntimeException: cannot do something else
execution(void de.scrum_master.app.FooService.start()) -> java.lang.RuntimeException: cannot start
execution(void de.scrum_master.app.FooService.pause()) -> java.lang.RuntimeException: cannot pause
execution(void de.scrum_master.app.FooService.stop()) -> java.lang.RuntimeException: cannot stop
关于java - AOP围绕外部库的重写方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27943872/
我在同一类中的方法之间进行方法调用并应用事务建议时遇到问题。 Spring Framework .NET 文档声明它支持基于组合和继承的代理,并且您可以强制 Spring 创建要实例化的基于继承的代理
我理解这些原则,但我很难看到实际应用程序在少数几个之外。请赐教;) 最佳答案 问任何支持人员:日志记录不是 AOP 的一个好的应用程序。他们不在乎应用程序内部调用了什么方法。他们关心应用程序正在执行的
我知道以前有人问过这个问题,但这是一年半前的事了,尽管我认为现在可能是重新提问的时候了。我也认识到它可能被视为主观的,但我想有一些客观的原因支持/反对 AOP。 我会对 感兴趣谁在使用 AOP 在软件
我想这个问题以前有人问过,但我无法立即找到相关的 SO 问题或其他地方的相关文章。 令我震惊的是,AOP 中的某些术语相当奇怪。看来我不是唯一一个-这个article ,例如,指出“不幸的是,AOP
面向切面编程可能的和严重的缺点是什么? 例如:新手的神秘调试(可读性影响) 最佳答案 工具链支持不佳 - 调试器、分析器等可能不了解 AOP,因此可能会在代码上工作,就好像所有方面都已被过程代码替换
这两种AOP框架的优缺点是什么?我使用 Unity 作为我的 aop 框架,但我猜想编译时 aop 框架(例如 postsharp)可能比运行时 aop 框架具有更好的性能?看起来运行时 aop 框架
我现在正在学习 spring aop,我不知道将上下文参数传递给建议。 请注意,我指的是 context 参数,而不是 normal 参数。 传递普通参数很简单,例如: a join point: p
来自类路径资源 [ApplicationContextAOP.xml] 的 XML 文档中的第 13 行无效;嵌套异常是 org.xml.sax.SAXParseException: cvc-comp
我使用 spring boot 2 和 spring security。 使用 aop,我搜索以获取调用该方法的用户。 @Aspect @Component public class LogAspec
我最近一直在一个非常简单的应用程序上尝试 Spring 的 AOP 功能,并且我坚持在适当的时间运行该方法,这意味着该部分中定义的方法应该在 中定义的方法之后运行 在我的代码中,这两个方法都在主方法中
我试图在网上找到如何通过 Ninject 使用 AOP 的例子。有人可以确认 AOP 在 Ninject 2 中是否可用而不使用外部库(即 CaSTLe Windsor?)。 如果可以的话,您能否发布
Aop配置已经在我的项目中完成了。为此添加了以下配置。问题是当下面的代码没有注释时,不会调用 formService 中的方法。因此我得到空指针异常。知道问题出在哪里吗?我附上了下面的代码.. AOP
我是 AOP 的新手。我遇到了这样的问题。 package org.suman.Aspect; import org.aspectj.lang.annotation.Aspect; import or
在我们的企业应用程序中,我们希望将日志记录、度量等横切关注点作为方面。我们已经准备好了 aspectj 建议(来自我们现有的 java 应用程序),但我没有找到将 aspectj 与 Grails 集
我正在向外部系统编写 Web 服务。 我的服务包装类有许多方法可以调用Web服务的所有soap接口(interface)。该调用可能会引发异常,然后该异常会自动触发重新连接到 Web 服务。 为了处理
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
我是 spring 框架的新手,正在尝试一些示例来理解 AOP,这是我到目前为止所做的,但它不起作用。 问题是我一添加 对于 spring.xml,我的构建失败说无法创建具有空指针异常的 bean。但
下面是我要创建的方面。我想将两个切入点表达式合并为一个。我已经看到这可以使用带注释的切入点来完成,但是 xml 中的相同语法失败了。谁能帮帮我? 提前致谢 最佳答案
我对 Spring 事务管理感到困惑。在我的应用程序中,我在服务类中使用 @Transactional 实现了事务管理。我配置的 spring.xml 如下:
我知道围绕 Controller 方法编写 AOP 建议的标准方法,并且如果在 Controller 方法中声明,您可以访问 HttpServletRequest arg。 但我的情况是我有一个翻译服
我是一名优秀的程序员,十分优秀!