简体   繁体   English

Spring AOP xml配置不起作用

[英]Spring AOP xml configuration doesn't work

I tried to write a simple AOP program and I configure the xml file like below. 我试图编写一个简单的AOP程序,并按如下所示配置xml文件。 But when I ran the program, the result seems not calling the advice but only execute non-advice part. 但是当我运行程序时,结果似乎没有调用建议,而是仅执行了非建议部分。

Here is the method class: 这是方法类:

public class MessageWriterStdOut {
    public void writeMessage() {
        System.out.print("World");
    }
} 

This is the bean class: 这是bean类:

public class MyBeanOfHelloWorld {
  private MessageWriterStdOut messageWriterStdOut;
    public void execute() {
        messageWriterStdOut.writeMessage();
    }
    public void setMessageWriterStdOut(MessageWriterStdOut messageWriterStdOut) {
        this.messageWriterStdOut = messageWriterStdOut;
    }
}

The advice class is like this: 咨询类是这样的:

public class MessageDecorator implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.print("Hello ");
        Object retVal = invocation.proceed();
        System.out.println("!");
        return retVal;
    }
}

The main method is : 主要方法是:

public class HelloWorldAopExample {
    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("/META-INF/spring/context-aop.xml");
        ctx.refresh();
        MyBeanOfHelloWorld myBeanOfHelloWorld = (MyBeanOfHelloWorld) ctx.getBean("myBeanOfHelloWorld");
        myBeanOfHelloWorld.execute();
    }
}

and the xml configuration is : 和xml配置是:

<aop:config>
        <aop:pointcut id="helloExecution"
            expression="execution(* com..playground..writeMessage*()) and args(invocation)" />
        <aop:aspect ref="advice">
            <aop:around pointcut-ref="helloExecution" method="invoke" />
        </aop:aspect>
    </aop:config>
    <bean id="advice" class="com..playground.aophelloworld.MessageDecorator" />
    <bean id="messageWriterStdOut"
        class="com..playground.aophelloworld.MessageWriterStdOut" />
    <bean id="myBeanOfHelloWorld" class="com..playground.aophelloworld.MyBeanOfHelloWorld">
        <property name="messageWriterStdOut" ref="messageWriterStdOut" />
    </bean>

But the result is still only "world", while the expected result is "hello world!" 但是结果仍然只是“世界”,而预期结果是“ hello world!”

In the xml configuration, why some of your package paths are having two dots? 在xml配置中,为什么您的某些包装路径有两个点? For example: 例如:

<aop:pointcut id="helloExecution"
        expression="execution(* com..playground..writeMessage*()) and args(invocation)" />

should be: 应该:

<aop:pointcut id="helloExecution"
        expression="execution(* com.playground.writeMessage*()) and args(invocation)" />

and some other places: 和其他一些地方:

<bean id="advice" class="com.playground.aophelloworld.MessageDecorator" />
<bean id="myBeanOfHelloWorld" class="com.playground.aophelloworld.MyBeanOfHelloWorld">
    <property name="messageWriterStdOut" ref="messageWriterStdOut" />

Updated: Can you try this: 更新:您可以尝试以下方法:

<aop:pointcut id="helloExecution"
        expression="execution(* com..playground.MessageWriterStdOut.writeMessage(..))" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM