Spring AOP 和 AspectJ。需要有关周围建议的建议/评论

发布于 2024-12-06 07:42:09 字数 1837 浏览 0 评论 0原文

我将其发布在另一个论坛上,想看看是否可以接触到更多人。

我正在开发一个由不同 Spring Web 应用程序组成的应用程序。

假设我们有:

  • ComponentA.jar
  • ComponentB.jar

和 WAR 文件:

  • Foo.war (包含 ComponentA)
  • Baa.war (包含 ComponentA 和 ComponentA) ComponentB)

我们使用 Logback 来记录我们的调试日志。因此,假设应用程序的各个类具有以下记录器声明:

private static final Log log = LoggerFactory.getLogger(NAME_OF_WAR_FILE + "." + NAME_OF_CONTAINING_COMPONENT + "." + PACKAGE.CLASS_NAME);

例如:

package a.b.c;

public class SomeClass {
   private static final Log log = LoggerFactory.getLogger("Foo.war" + "." + "ComponentA" + "." + SomeClass.class);
}

package x.y.z;

public class SomeOtherClass {

   private static final Log log = LoggerFactory.getLogger("Baa.war" + "." + "ComponentA" + "." + SomeOtherClass .class);
}

假设 war 文件和组件的名称是由属性设置的,而不是硬编码的。

是否有可能有一个方面和建议可以执行如下操作(伪,因为我不确定是否可以完成):

@Aspect
public class TheAspect{

   @Around("execution of a method")
   public Object aroundSomething(ProceedingJoinPoint pjp){

      Log log = get the log instance from the class that this advice is running on

      if(log.isDebugEnabled())
         // log something

      Object o = pjp.proceed();

       if(log.isDebugEnabled())
         // log something else

     return o;

   }
}

这里的要点是使用包含以下内容的类实例的日志写入日志文件被通知拦截的方法。

该应用程序呈现为由 Foo.war 和 Baa.war 组成的单个 Web 应用程序。 Foo.war 和 Baa.war 都写入同一个日志文件。

示例:

2011-09-22 14:35:35.159 MDT,DEBUG,Foo.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentB.x.y.z.SomeOtherClass,Hello World Debug message

提前致谢。

I posted this on another forum and wanted to see if I can reach more people.

I am working on an application that consists of different Spring web apps.

Say we have:

  • ComponentA.jar
  • ComponentB.jar

And WAR files:

  • Foo.war (contains ComponentA)
  • Baa.war (contains ComponentA &
    ComponentB)

We are using Logback to log to our debug log. So say that the various classes of the application have the the following logger declaration:

private static final Log log = LoggerFactory.getLogger(NAME_OF_WAR_FILE + "." + NAME_OF_CONTAINING_COMPONENT + "." + PACKAGE.CLASS_NAME);

So example:

package a.b.c;

public class SomeClass {
   private static final Log log = LoggerFactory.getLogger("Foo.war" + "." + "ComponentA" + "." + SomeClass.class);
}

package x.y.z;

public class SomeOtherClass {

   private static final Log log = LoggerFactory.getLogger("Baa.war" + "." + "ComponentA" + "." + SomeOtherClass .class);
}

Assume that the name of the war file and component is set by a property and not hard-coded.

Is it possible to have an Aspect and Advice that can do something like the following (pseudo since I'm not sure it can be done):

@Aspect
public class TheAspect{

   @Around("execution of a method")
   public Object aroundSomething(ProceedingJoinPoint pjp){

      Log log = get the log instance from the class that this advice is running on

      if(log.isDebugEnabled())
         // log something

      Object o = pjp.proceed();

       if(log.isDebugEnabled())
         // log something else

     return o;

   }
}

The point here is to write to the log file using the log of the class instance which contains the method which is being intercepted by the Advice.

The application is presented as a single web app composed of Foo.war and Baa.war. Both Foo.war and Baa.war write to the same log file.

Example:

2011-09-22 14:35:35.159 MDT,DEBUG,Foo.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentA.a.b.c.SomeClass,Hello World Debug message
2011-09-22 14:35:35.159 MDT,DEBUG,Baa.war.ComponentB.x.y.z.SomeOtherClass,Hello World Debug message

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2024-12-13 07:42:10

您可以在 aroundSomething 方法中使用 thisJoinPoint

要获取类名:

Signature sig = thisJoinPoint.getSignature();
String className = sig.getDeclaringTypeName();

您还可以获取类对象:

Class<?> type = sig.getDeclaringType();

也许您可以使用包来识别您的 war 文件:

Package pack type.getPackage();

You can use thisJoinPoint inside your aroundSomething method.

To get the class name:

Signature sig = thisJoinPoint.getSignature();
String className = sig.getDeclaringTypeName();

You can also get the class object:

Class<?> type = sig.getDeclaringType();

And maybe you can use the package to identify your war file:

Package pack type.getPackage();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文