org.springframework.aop.framework.ProxyFactoryBean#getObject
public Object getObject() throws BeansException {
//初始化通知器链,通知器链封装了一些列拦截器,这些拦截器从配置文件中获取
this.initializeAdvisorChain();
if (this.isSingleton()) {
return this.getSingletonInstance();
} else {
if (this.targetName == null) {
this.logger.warn("Using non-singleton proxies with singleton targets is often undesirable. Enable prototype proxies by setting the 'targetName' property.");
}
return this.newPrototypeInstance();
}
}
org.springframework.aop.framework.ProxyFactoryBean#initializeAdvisorChain
private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
if (!this.advisorChainInitialized) {
if (!ObjectUtils.isEmpty(this.interceptorNames)) {
if (this.beanFactory == null) {
throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) - cannot resolve interceptor names " + Arrays.asList(this.interceptorNames));
}
if (this.interceptorNames[this.interceptorNames.length - 1].endsWith("*") && this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) {
throw new AopConfigException("Target required after globals");
}
String[] arr$ = this.interceptorNames;
int len$ = arr$.length;
for(int i$ = 0; i$ < len$; ++i$) {
String name = arr$[i$];
if (this.logger.isTraceEnabled()) {
this.logger.trace("Configuring advisor or advice '" + name + "'");
}
if (name.endsWith("*")) {
if (!(this.beanFactory instanceof ListableBeanFactory)) {
throw new AopConfigException("Can only use global advisors or interceptors with a ListableBeanFactory");
}
//这里是添加Advisor链的调用,是通过interceptorNames属性进行配置的
this.addGlobalAdvisor((ListableBeanFactory)this.beanFactory, name.substring(0, name.length() - "*".length()));
} else {
//如果程序走这边,那么需要加入命名的拦截器advice,并且需要检查这个Bean
//是singleton还是prototype
Object advice;
if (!this.singleton && !this.beanFactory.isSingleton(name)) {
advice = new ProxyFactoryBean.PrototypePlaceholderAdvisor(name);
} else {
advice = this.beanFactory.getBean(name);
}
this.addAdvisorOnChainCreation(advice, name);
}
}
}
this.advisorChainInitialized = true;
}
}
org.springframework.aop.framework.ProxyFactoryBean#getSingletonInstance
private synchronized Object getSingletonInstance() {
if (this.singletonInstance == null) {
this.targetSource = this.freshTargetSource();
if (this.autodetectInterfaces && this.getProxiedInterfaces().length == 0 && !this.isProxyTargetClass()) {
//根据AOP框架来判断需要代理的接口
Class<?> targetClass = this.getTargetClass();
if (targetClass == null) {
throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
}
//这里设置代理对象的接口
this.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
}
super.setFrozen(this.freezeProxy);
//通过createAopProxy返回的AOPProxy来得到代理对象
this.singletonInstance = this.getProxy(this.createAopProxy());
}
return this.singletonInstance;
}
//如果目标对象是接口类,则通过jdk来生成对象,否则使用CGLIB
return (AopProxy)(targetClass.isInterface() ? new JdkDynamicAopProxy(config) : DefaultAopProxyFactory.CglibProxyFactory.createCglibProxy(config));