Spring AOP 和 AspectJ。需要有关周围建议的建议/评论
我将其发布在另一个论坛上,想看看是否可以接触到更多人。
我正在开发一个由不同 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
aroundSomething
方法中使用thisJoinPoint
。要获取类名:
您还可以获取类对象:
也许您可以使用包来识别您的 war 文件:
You can use
thisJoinPoint
inside youraroundSomething
method.To get the class name:
You can also get the class object:
And maybe you can use the package to identify your war file: