包装org.example; @方面 @order(value = 2) @成分 公共课程SayAspect { @declareparents(value =“ org.example ..*+”,defaultimpl = defaultgoodbye.class) 私人告别

问题描述 投票:0回答:1
您可能正在使用Intellij Idea和Mean
( expected

,而不是主题行中所述。

spring intellij-idea aop aspectj spring-aop
1个回答
0
投票

这是Idea extactJ和春季AOP支持的故障。如果您只是忽略它,那么方面仍然可以正常工作。我尝试过,它效果很好:
) expected

package org.example; public interface GoodBye { String getGoodBye(); } IDEA code helppackage org.example; public class DefaultGoodBye implements GoodBye { @Override public String getGoodBye() { return "Farewell"; } }

package org.example; import org.springframework.stereotype.Component; @Component public class MyComponent { public void sayHello() { System.out.println("Hello"); } public void sayGoodBye() { System.out.println("Good bye"); } }

package org.example; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.DeclareParents; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.Arrays; @Aspect @Order(value = 2) @Component public class SayAspect { @DeclareParents(value = "org.example..*", defaultImpl = DefaultGoodBye.class) private GoodBye goodBye; private int count = 0; @Before("execution(* org.example.MyComponent.sayHello())") public void beforeHello(JoinPoint joinPoint) { System.out.println("Before Hello"); System.out.println(Arrays.toString(joinPoint.getTarget().getClass().getInterfaces())); System.out.println(Arrays.toString(joinPoint.getThis().getClass().getInterfaces())); } }
Console Log说:
package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class DemoApplication {
  public static void main(String[] args) {
    try (ConfigurableApplicationContext appContext = SpringApplication.run(DemoApplication.class, args)) {
      doStuff(appContext);
    }
  }

  private static void doStuff(ConfigurableApplicationContext appContext) {
    MyComponent myComponent = appContext.getBean(MyComponent.class);
    myComponent.sayHello();
    myComponent.sayGoodBye();
    System.out.println(((GoodBye) myComponent).getGoodBye());
  }
}
你可以看到,
the以前的建议开始,
组件代理实现
Before Hello [] [interface org.example.GoodBye, interface org.springframework.aop.SpringProxy, interface org.springframework.aop.framework.Advised, interface org.springframework.cglib.proxy.Factory] Hello Good bye Farewell

接口,

可以将代理组件施加到
GoodBye

中,并调用其

GoodBye
    方法会产生默认实现的预期结果。
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.