1 16 17 package org.springframework.aop.config; 18 19 import java.util.List ; 20 21 import org.w3c.dom.Node ; 22 23 import org.springframework.aop.framework.ProxyFactoryBean; 24 import org.springframework.beans.MutablePropertyValues; 25 import org.springframework.beans.factory.config.BeanDefinition; 26 import org.springframework.beans.factory.config.BeanDefinitionHolder; 27 import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; 28 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 29 import org.springframework.beans.factory.support.ManagedList; 30 import org.springframework.beans.factory.support.RootBeanDefinition; 31 import org.springframework.beans.factory.xml.BeanDefinitionDecorator; 32 import org.springframework.beans.factory.xml.ParserContext; 33 import org.springframework.util.ClassUtils; 34 import org.springframework.util.StringUtils; 35 36 56 public abstract class AbstractInterceptorDrivenBeanDefinitionDecorator implements BeanDefinitionDecorator { 57 58 public final BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definitionHolder, ParserContext parserContext) { 59 BeanDefinitionRegistry registry = parserContext.getRegistry(); 60 61 String existingBeanName = definitionHolder.getBeanName(); 63 BeanDefinition existingDefinition = definitionHolder.getBeanDefinition(); 64 65 BeanDefinition interceptorDefinition = createInterceptorDefinition(node); 67 68 String interceptorName = existingBeanName + "." + getInterceptorNameSuffix(interceptorDefinition); 70 BeanDefinitionReaderUtils.registerBeanDefinition(new BeanDefinitionHolder(interceptorDefinition, interceptorName), registry); 71 72 BeanDefinitionHolder result = definitionHolder; 73 74 if (!isProxyFactoryBeanDefinition(existingDefinition)) { 75 76 RootBeanDefinition proxyDefinition = new RootBeanDefinition(); 78 proxyDefinition.setBeanClass(ProxyFactoryBean.class); 80 81 MutablePropertyValues mpvs = new MutablePropertyValues(); 83 proxyDefinition.setPropertyValues(mpvs); 84 85 mpvs.addPropertyValue("target", existingDefinition); 87 88 ManagedList interceptorList = new ManagedList(); 90 mpvs.addPropertyValue("interceptorNames", interceptorList); 91 92 result = new BeanDefinitionHolder(proxyDefinition, existingBeanName); 93 } 94 95 addInterceptorNameToList(interceptorName, result.getBeanDefinition()); 96 97 return result; 98 99 } 100 101 private void addInterceptorNameToList(String interceptorName, BeanDefinition beanDefinition) { 102 List list = (List ) beanDefinition.getPropertyValues().getPropertyValue("interceptorNames").getValue(); 103 list.add(interceptorName); 104 } 105 106 private boolean isProxyFactoryBeanDefinition(BeanDefinition existingDefinition) { 107 return existingDefinition.getBeanClassName().equals(ProxyFactoryBean.class.getName()); 108 } 109 110 protected String getInterceptorNameSuffix(BeanDefinition interceptorDefinition) { 111 return StringUtils.uncapitalize(ClassUtils.getShortName(interceptorDefinition.getBeanClassName())); 112 } 113 114 118 protected abstract BeanDefinition createInterceptorDefinition(Node node); 119 120 } 121 | Popular Tags |