原创

spring aop 理解

Aop
如果是接口:用java动态代理
如果不是接口:用Cglib

动态代理后,类$Proxy默认继承Proxy,因为java单继承机制,所以只有接口才能用java动态代理

获取bean的方式
new AnnotationConfigApplicationContext

继承ApplicationContextAware
ApplicationContext

@Autowired
private ApplicationContext context;

	@Override
	public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
		if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
			Class<?> targetClass = config.getTargetClass();
			if (targetClass == null) {
				throw new AopConfigException("TargetSource cannot determine target class: " +
						"Either an interface or a target is required for proxy creation.");
			}
			if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
				return new JdkDynamicAopProxy(config);
			}
			return new ObjenesisCglibAopProxy(config);
		}
		else {
			return new JdkDynamicAopProxy(config);
		}
	}
正文到此结束