我想了解Spring代理機制,我有一件事情的問題。 我有接口:春天AOP代理和接口實現
public interface MyInterface{
void myMethod();
}
和實現類:
@Component
public class MyBean implements MyInterface{
@Override
public void myMethod(){
//do something
}
}
現在我創造方面,例如:
@Aspect
@Component
public class LogAspect {
@Before("execution(public * *(..))")
public void logBefore() {
System.out.println("Before aspect");
}
}
而且我有簡單的入門級:
@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class SpringAopApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
SpringAopApplication.class);
MyBean bean = ctx.getBean(MyBean.class);
// MyInterface bean = ctx.getBean(MyInterface.class); //works
bean.myMethod();
ctx.close();
}
}
A根據Spring文檔,我們可以閱讀:
如果要代理的目標對象實現至少一個接口 那麼將使用JDK動態代理。所有由目標類型實現的接口 將被代理。如果目標對象 未實現任何接口,則將創建CGLIB代理。
但我得到了一個錯誤類型[爲myBean]定義沒有合格豆。它只有當我啓用CGLib代理@EnableAspectJAutoProxy(proxyTargetClass = true)
時纔有效。 有人可以解釋我在這裏失蹤了嗎?爲什麼在使用AOP時未發現MyBean? ctx.getBean(MyInterface.class)
的作品,但我無法想象這種接口的許多實現的情況。