java Spring AOP 配置(XML)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7571371/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:33:27  来源:igfitidea点击:

Spring AOP Configuration (XML)

javaxmlspringaop

提问by IAmYourFaja

I am experimenting with Spring AOP for the first time and get stuck in the XML configuration. I'm trying to get a mock version of AOP-based "logging" up and running, using a MethodInterceptorto wrap specific method calls and do some simple System.out.printlnstatements before and after those method invocations. Simple stuff, right?

我第一次尝试 Spring AOP 并陷入 XML 配置。我正在尝试启动并运行基于 AOP 的“日志记录”的模拟版本,使用 aMethodInterceptor来包装特定的方法调用System.out.println并在这些方法调用之前和之后执行一些简单的语句。简单的东西,对吧?

So my project has many classes, two of them are Fizzand Buzz. Fizz has a method named foo()and Buzz has a method named wapap(). Every time these methods are invoked at runtime, I want my LoggingInterceptorto execute its invoke() method around them:

所以我的项目有很多类,其中两个是FizzBuzz。Fizz 有一个名为 的方法foo(),Buzz 有一个名为 的方法wapap()。每次在运行时调用这些方法时,我都希望LoggingInterceptor围绕它们执行其 invoke() 方法:

public class LoggingInterceptor implements MethodInterceptor
{
    public Object invoke(MethodInvocation methodInvocation)
    {
        try
        {
            System.out.println("About to call a special method.");
            Object result = methodInvocation.proceed();
            return result;
        }
        finally
        {
            System.out.println("Finished executing the special method.");
        }
    }
}

So I understand the concepts of advice (my interceptor impl), pointcuts (the methods that will have advice executed around them), and pointcut advisors (bindings between advice and pointcuts).

所以我理解建议(我的拦截器实现)、切入点(将在它们周围执行建议的方法)和切入点顾问(建议和切入点之间的绑定)的概念。

I'm just struggling tying it altogether in a simple XML config.

我只是在努力将它完全绑定在一个简单的 XML 配置中。

Here's what I have so far, but I know it's missing pointcut and pointcut advisor definitions, and possibly more.

这是我到目前为止所拥有的,但我知道它缺少切入点和切入点顾问定义,可能还有更多。

<beans default-autowire="no" >
    <bean name="loggingInterceptor" class="org.me.myproject.aop.LoggingInterceptor"/>   
</beans>

What am I missing here to make this specific to Fizz::foo() and Buzz::wapap() calls?

我在这里错过了什么来使这个特定于 Fizz::foo() 和 Buzz::wapap() 调用?

Any nudges in the right direction are enormously appreciated!

任何朝着正确方向的推动都非常感谢!

回答by Tomasz Nurkiewicz

Add this:

添加这个:

<aop:config>
    <aop:advisor advice-ref="loggingInterceptor" pointcut="execution(public * Fizz.foo(..))"/>
    <aop:advisor advice-ref="loggingInterceptor" pointcut="execution(public * Buzz.wapap(..))"/>
</aop:config>

You also need to add AOP namespace declaration in version appropriate to your framework:

您还需要在适合您的框架的版本中添加 AOP 命名空间声明:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        ">

Also consider using @AspectJ aspects and see this question: Spring: Standard Logging aspect (interceptor).

还可以考虑使用 @AspectJ 方面并查看这个问题:Spring: Standard Logging aspect (interceptor)

回答by danny.lesnik

If you are using Spring 2.5+ you can use annotation to and create your advice and Pointcuts.

如果您使用的是 Spring 2.5+,您可以使用注解来创建您的建议和切入点。

Create class with @Aspectannotation.

使用@Aspect注释创建类。

Create @PointCutfor specific class and specific method and then create @Aroundadvice.

创建@PointCut特定类和具体方法,然后创建@Around建议。

You can read short tutorial how to do it here:

您可以在此处阅读如何操作的简短教程:

http://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/

http://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/

It' very easy to implement.

这很容易实现。