您可能正在使用Intellij Idea和Mean
( expected
,而不是主题行中所述。
) expected
package org.example;
public interface GoodBye {
String getGoodBye();
}
package 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