我知道在春天有两种创建代理的方法,一种是使用 JDK代理,另一种是使用 CGLIB 。我也知道这两种代理之间的区别。但我无法理解春天的 AOP 。
例如:
public interface Performance {
void perform();
}
public class AnotherPerformance implements Performance {
@Override
public void perform() {
System.out.println("AnotherPerformance is performing");
}
}
}
方面 -
@Component
@Aspect
public class Audience {
@Pointcut("execution(* com.xxx.aspect.Performance.perform(..))")
public void perform(){
}
@Before("perform()")
public void silenceCellphones(){
System.out.println("silence cell phones");
}
@Before("perform()")
public void takeSeats(){
System.out.println("Taking seats");
}
@AfterReturning("perform()")
public void applause(){
System.out.println("CLAP CLAP CLAP!!!!");
}
@AfterThrowing("perform()")
public void demandRefund(){
System.out.println("Demanding refund!!!!");
}
@Around("perform()")
public void watchPerformance(ProceedingJoinPoint joinPoint){
try {
System.out.println("silence cell phones");
System.out.println("Taking seats");
joinPoint.proceed();
System.out.println("CLAP CLAP CLAP!!!!");
} catch(Throwable e){
System.out.println("Demanding refund!!!!");
}
}
}
客户端代码如下:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xxx.xml");
AnotherPerformance performance = ctx.getBean("anotherPerformance",AnotherPerformance.class);
}
从中删除<aop:aspectj-autoproxy proxy-target-class="true"/>
时
弹簧配置文件,抛出错误:
Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'anotherPerformance' is expected to be of type 'com.aspect.xmlconfig.AnotherPerformance' but was actually of type 'com.sun.proxy.$Proxy2'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:378)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084)
at com.aspect.xmlconfig.AnotherMain.main(AnotherMain.java:22)
20:47:21.028 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Failed to convert bean 'anotherPerformance' to required type 'com.springinaction.aspect.xmlconfig.AnotherPerformance'
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'com.sun.proxy.$Proxy2 implementing com.springinaction.aspect.Performance,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'com.aspect.xmlconfig.AnotherPerformance'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'com.sun.proxy.$Proxy2 implementing com.aspect.Performance,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,org.springframework.core.DecoratingProxy' to required type 'com.aspect.xmlconfig.AnotherPerformance': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:40)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1084)
at com.aspect.xmlconfig.AnotherMain.main(AnotherMain.java:22)
proxy-target-class=true
使用 CGLIB 创建代理,但这里的类Perfromance
是一个接口,为什么spring无法创建代理对象使用JDK动态代理?