1 16 17 package org.springframework.aop.config; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator; 23 import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; 24 import org.springframework.beans.factory.config.BeanDefinition; 25 import org.springframework.beans.factory.parsing.BeanComponentDefinition; 26 import org.springframework.beans.factory.support.BeanDefinitionRegistry; 27 import org.springframework.beans.factory.support.RootBeanDefinition; 28 import org.springframework.beans.factory.xml.ParserContext; 29 import org.springframework.core.Ordered; 30 import org.springframework.util.Assert; 31 import org.springframework.util.ClassUtils; 32 33 47 public abstract class AopNamespaceUtils { 48 49 52 public static final String AUTO_PROXY_CREATOR_BEAN_NAME = 53 "org.springframework.aop.config.internalAutoProxyCreator"; 54 55 59 public static final String ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME = 60 "org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"; 61 62 63 66 private static final List APC_PRIORITY_LIST = new ArrayList (); 67 68 71 static { 72 APC_PRIORITY_LIST.add(DefaultAdvisorAutoProxyCreator.class.getName()); 73 APC_PRIORITY_LIST.add(AspectJAwareAdvisorAutoProxyCreator.class.getName()); 74 APC_PRIORITY_LIST.add(ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME); 75 } 76 77 78 public static void registerAutoProxyCreatorIfNecessary(ParserContext parserContext, Object sourceElement) { 79 registryOrEscalateApcAsRequired(DefaultAdvisorAutoProxyCreator.class, parserContext, sourceElement); 80 } 81 82 public static void registerAspectJAutoProxyCreatorIfNecessary(ParserContext parserContext, Object sourceElement) { 83 registryOrEscalateApcAsRequired( 84 AspectJAwareAdvisorAutoProxyCreator.class, parserContext, sourceElement); 85 } 86 87 public static void registerAtAspectJAutoProxyCreatorIfNecessary(ParserContext parserContext, Object sourceElement) { 88 Class cls = getAspectJAutoProxyCreatorClassIfPossible(); 89 registryOrEscalateApcAsRequired(cls, parserContext, sourceElement); 90 } 91 92 private static void registryOrEscalateApcAsRequired(Class cls, ParserContext parserContext, Object sourceElement) { 93 Assert.notNull(parserContext, "ParserContext must not be null"); 94 BeanDefinitionRegistry registry = parserContext.getRegistry(); 95 96 if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) { 97 BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME); 98 if (cls.getName().equals(apcDefinition.getBeanClassName())) { 99 return; 100 } 101 int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName()); 102 int requiredPriority = findPriorityForClass(cls.getName()); 103 if (currentPriority < requiredPriority) { 104 apcDefinition.setBeanClassName(cls.getName()); 105 } 106 } 107 108 else { 109 RootBeanDefinition beanDefinition = new RootBeanDefinition(cls); 110 beanDefinition.setSource(parserContext.extractSource(sourceElement)); 111 beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); 112 registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition); 113 beanDefinition.getPropertyValues().addPropertyValue("order", new Integer (Ordered.HIGHEST_PRECEDENCE)); 114 BeanComponentDefinition componentDefinition = 116 new BeanComponentDefinition(beanDefinition, AUTO_PROXY_CREATOR_BEAN_NAME); 117 parserContext.registerComponent(componentDefinition); 118 } 119 } 120 121 public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistry registry) { 122 if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) { 123 BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME); 124 definition.getPropertyValues().addPropertyValue("proxyTargetClass", Boolean.TRUE); 125 } 126 } 127 128 private static Class getAspectJAutoProxyCreatorClassIfPossible() { 129 try { 130 return ClassUtils.forName(ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME); 131 } 132 catch (Throwable ex) { 133 throw new IllegalStateException ( 134 "Unable to load class [" + ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME + 135 "]. Are you running on Java 1.5+? Root cause: " + ex); 136 } 137 } 138 139 private static final int findPriorityForClass(String className) { 140 Assert.notNull(className, "Class name must not be null"); 141 for (int i = 0; i < APC_PRIORITY_LIST.size(); i++) { 142 String str = (String ) APC_PRIORITY_LIST.get(i); 143 if (className.equals(str)) { 144 return i; 145 } 146 } 147 throw new IllegalArgumentException ( 148 "Class name [" + className + "] is not a known auto-proxy creator class"); 149 } 150 151 } 152 | Popular Tags |