1 16 17 package org.springframework.aop.config; 18 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 22 import org.springframework.aop.framework.autoproxy.AutoProxyUtils; 23 import org.springframework.aop.scope.ScopedProxyFactoryBean; 24 import org.springframework.beans.factory.config.BeanDefinition; 25 import org.springframework.beans.factory.config.BeanDefinitionHolder; 26 import org.springframework.beans.factory.support.AbstractBeanDefinition; 27 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 28 import org.springframework.beans.factory.support.RootBeanDefinition; 29 import org.springframework.beans.factory.xml.BeanDefinitionDecorator; 30 import org.springframework.beans.factory.xml.ParserContext; 31 32 40 class ScopedProxyBeanDefinitionDecorator implements BeanDefinitionDecorator { 41 42 private static final String PROXY_TARGET_CLASS = "proxy-target-class"; 43 44 private static final String TARGET_NAME_PREFIX = "scopedTarget."; 45 46 47 public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition, ParserContext parserContext) { 48 String originalBeanName = definition.getBeanName(); 49 BeanDefinition targetDefinition = definition.getBeanDefinition(); 50 BeanDefinitionRegistry registry = parserContext.getRegistry(); 51 52 String targetBeanName = TARGET_NAME_PREFIX + originalBeanName; 55 RootBeanDefinition scopedProxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class); 56 scopedProxyDefinition.getPropertyValues().addPropertyValue("targetBeanName", targetBeanName); 57 58 boolean proxyTargetClass = (!(node instanceof Element ) || 59 Boolean.valueOf(((Element ) node).getAttribute(PROXY_TARGET_CLASS)).booleanValue()); 60 if (proxyTargetClass) { 61 targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE); 62 } 64 else { 65 scopedProxyDefinition.getPropertyValues().addPropertyValue("proxyTargetClass", Boolean.FALSE); 66 } 67 68 if (targetDefinition instanceof AbstractBeanDefinition) { 70 ((AbstractBeanDefinition) targetDefinition).setAutowireCandidate(false); 71 } 72 73 registry.registerBeanDefinition(targetBeanName, targetDefinition); 75 76 return new BeanDefinitionHolder(scopedProxyDefinition, originalBeanName); 79 } 80 81 } 82 | Popular Tags |