在春天用AOP记录? [英] logging with AOP in spring?
问题描述
我是新来的,在我的办公室里。所以我没有指导。
I am new to spring in my office . So there is no guidance for me.
我需要使用<$ c来实现 AOP
的日志记录$ C> log4j的。
I need to implement the logging with the AOP
using the log4j
.
我在基本的 spring MVC $ c $中实现了没有
AOP
的日志记录c>示例?
I have implemented the logging without AOP
in basic spring MVC
example ?
还使用 aspectJ在
没有记录(只是 AOP
中做了小样本 Sysout
)?
Also did the small sample in AOP
using the aspectJ
without logging (just made the Sysout
) ?
我不知道如何整合它?
任何人都可以给我一个启动想法吗?
Can any one please give me a start up idea?
很好的答案绝对值得赞赏.. 。
Good answers are definitely appreciated ...
推荐答案
Spring让我们很容易使用AOP。这是一个简单的日志记录示例:
Spring makes it really easy for us to make use of AOP. Here's a simple logging example:
@Aspect
public class MyLogger {
private Logger log = Logger.getLogger(getClass());
@After("execution(* com.example.web.HomeController.*(..))")
public void log(JoinPoint point) {
log.info(point.getSignature().getName() + " called...");
}
}
然后只需配置applicationContext.xml(或等效的) :
Then simply configure your applicationContext.xml (or equivalent):
<aop:aspectj-autoproxy>
<aop:include name="myLogger"/>
</aop:aspectj-autoproxy>
<bean id="myLogger" class="com.example.aspect.MyLogger"/>
您会在MyLogger类中注意到我指定了 @After
就在方法的正上方。这称为建议,它基本上指定这个'log'方法在之后被称为方法。其他选项包括 @Before,@ Around,@ AfterThrowing
。
You'll notice in the MyLogger class that I specified @After
right above the method. This is called the advice and it basically specifies that this 'log' method will be called after the method in question. Other options include @Before, @Around, @AfterThrowing
.
表达式执行(* com.example.web.HomeController。*(..))
是称为切入点表达式并指定我们的目标(在本例中为HomeController类的所有方法)。
The expression "execution(* com.example.web.HomeController.*(..))"
is called a pointcut expression and specifies what we're targeting (in this case all methods of the HomeController class).
PS aop
命名空间( xmlns:aop =http://www.springframework.org/schema/aop
)并且需要将模式位置(依赖于版本)添加到顶部的applicationContext.xml中。这是我的设置:
P.S. The aop
namespace (xmlns:aop="http://www.springframework.org/schema/aop"
) and the schema location (version dependent) would need to be added to your applicationContext.xml right at the top. Here is my setup:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
这篇关于在春天用AOP记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!