编程语言
首页 > 编程语言> > java – 为什么这个Spring Aspect不能像方法参数一样打印?

java – 为什么这个Spring Aspect不能像方法参数一样打印?

作者:互联网

Let me first foremention, the problem I’m facing is with the
interceptThoughts(String thoughts)
method, from the first code block, not printing

我正在运行Spring in Action的教程.有一个魔术师类用interceptThoughts(String ideas)和getThoughts()方法实现MindReader接口

@Aspect
public class Magician implements MindReader {

    private String thoughts;

    @Pointcut("execution(* com.underdogdevs.myspringaspectj." 
            + "Thinker.thinkOfSomething(String)) && args(thoughts)")
    public void thinking(String thoughts) {
    }

    @Override
    @Before("thinking(thoughts)") 
    public void interceptThoughts(String thoughts) {
        System.out.println("Intercepting volunteer's thoughts : " + thoughts);  
        this.thoughts = thoughts;
    }

    @Override
    public String getThoughts() {
        return thoughts;
    }
}

该方面应该通过方法thinkOfSomething(字符串思想)阅读实现Thinker接口的志愿者的思想

public class Volunteer implements Thinker {

    private String thoughts;

    @Override
    public void thinkOfSomething(String thoughts) {
        this.thoughts = thoughts;
        System.out.println("Something");
    }

    public String getThoughts() {
        return thoughts;
    }
}

我有我的java BeanConfig与魔术师和志愿者

@Configuration
public class BeanConfig {

    @Bean
    public MindReader magician() {
        return new Magician();
    }

    @Bean
    public Thinker volunteer() {
        return new Volunteer();
    }  
}

而我正在尝试运行它以获得魔术师方法在interceptThoughts方法中打印线

public class App {
    public static void main(String[] args) {
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("spring-idol.xml");

        System.out.println();
        Thinker volunteer = (Thinker)context.getBean("volunteer");
        volunteer.thinkOfSomething("This is what I'm thinking");
    }
}

>没有Eorr
>没有例外
> @Pointcut中的包是正确的(执行(在Magician方面)
>我在Spring config xml中有这两个项目

<context:component-scan base-package="com.underdogdevs.myspringaspectj" />
<aop:aspectj-autoproxy />

The problem is the @Before from the Magician aspect isn’t printing as it should. Am I missing something here? Why is it not printing? I have other aspect methods that take no arguments and run just fine. Am I not passing the parameter value correctly?

解决方法:

试试这个

@Configuration
public class BeanConfig {

    @Bean
    public Magician magician() {
        return new Magician();
    }
...

我不知道它是否在Spring的文档中,但很明显,当Spring分析返回类型的魔术师()时,它是MindReader Spring时无法看到任何注释.

标签:java,spring,aop,aspects
来源: https://codeday.me/bug/20190629/1323514.html