1 16 17 package org.springframework.aop.aspectj.annotation; 18 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.aspectj.lang.reflect.PerClauseKind; 24 25 import org.springframework.aop.Advisor; 26 import org.springframework.aop.aspectj.AspectJProxyUtils; 27 import org.springframework.aop.framework.ProxyCreatorSupport; 28 import org.springframework.beans.BeanUtils; 29 import org.springframework.beans.factory.support.DefaultListableBeanFactory; 30 import org.springframework.beans.factory.support.RootBeanDefinition; 31 import org.springframework.util.Assert; 32 import org.springframework.util.ClassUtils; 33 34 48 public class AspectJProxyFactory extends ProxyCreatorSupport { 49 50 51 private static final Map aspectCache = new HashMap (); 52 53 private final AspectJAdvisorFactory aspectFactory = new ReflectiveAspectJAdvisorFactory(); 54 55 56 59 public AspectJProxyFactory() { 60 } 61 62 67 public AspectJProxyFactory(Object target) { 68 Assert.notNull(target, "Target object must not be null"); 69 setInterfaces(ClassUtils.getAllInterfaces(target)); 70 setTarget(target); 71 } 72 73 77 public AspectJProxyFactory(Class [] interfaces) { 78 setInterfaces(interfaces); 79 } 80 81 82 88 public void addAspect(Object aspect) { 89 Class aspectType = aspect.getClass(); 90 String beanName = aspectType.getName(); 91 AspectMetadata am = createAspectMetadata(aspectType, beanName); 92 if (am.getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON) { 93 throw new IllegalArgumentException ("Aspect type '" + aspectType.getName() + "' is not a singleton aspect."); 94 } 95 MetadataAwareAspectInstanceFactory instanceFactory = 96 new SingletonMetadataAwareAspectInstanceFactory(aspect, beanName); 97 addAdvisorsFromAspectInstanceFactory(instanceFactory); 98 } 99 100 103 public void addAspect(Class aspectType) { 104 String beanName = aspectType.getName(); 105 AspectMetadata am = createAspectMetadata(aspectType, beanName); 106 MetadataAwareAspectInstanceFactory instanceFactory = createAspectInstanceFactory(am, aspectType, beanName); 107 addAdvisorsFromAspectInstanceFactory(instanceFactory); 108 } 109 110 115 private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) { 116 List advisors = this.aspectFactory.getAdvisors(instanceFactory); 117 this.addAllAdvisors((Advisor[]) advisors.toArray(new Advisor[advisors.size()])); 118 makeAdvisorChainAspectJCapableIfNecessary(); 119 } 120 121 124 private AspectMetadata createAspectMetadata(Class aspectType, String beanName) { 125 AspectMetadata am = new AspectMetadata(aspectType, beanName); 126 if (!am.getAjType().isAspect()) { 127 throw new IllegalArgumentException ("Class [" + aspectType.getName() + "] is not a valid aspect type"); 128 } 129 return am; 130 } 131 132 137 private MetadataAwareAspectInstanceFactory createAspectInstanceFactory(AspectMetadata am, Class aspectType, String beanName) { 138 MetadataAwareAspectInstanceFactory instanceFactory = null; 139 if (am.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { 140 Object instance = getSingletonAspectInstance(aspectType); 141 instanceFactory = new SingletonMetadataAwareAspectInstanceFactory(instance, beanName); 142 } 143 else { 144 DefaultListableBeanFactory bf = getPrototypeAspectBeanFactory(aspectType, beanName); 146 instanceFactory = new PrototypeAspectInstanceFactory(bf, beanName); 147 } 148 return instanceFactory; 149 } 150 151 156 private void makeAdvisorChainAspectJCapableIfNecessary() { 157 if (AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(getAdvisorsInternal())) { 158 updateAdvisorArray(); 159 adviceChanged(); 160 } 161 } 162 163 167 private DefaultListableBeanFactory getPrototypeAspectBeanFactory(Class aspectType, String beanName) { 168 DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); 169 RootBeanDefinition definition = new RootBeanDefinition(aspectType); 170 definition.setSingleton(false); 171 bf.registerBeanDefinition(beanName, definition); 172 return bf; 173 } 174 175 179 private Object getSingletonAspectInstance(Class aspectType) { 180 synchronized (aspectCache) { 181 Object instance = aspectCache.get(aspectType); 182 if (instance != null) { 183 return instance; 184 } 185 instance = BeanUtils.instantiateClass(aspectType); 186 aspectCache.put(aspectType, instance); 187 return instance; 188 } 189 } 190 191 192 200 public <T> T getProxy() { 201 return (T) createAopProxy().getProxy(); 202 } 203 204 212 public <T> T getProxy(ClassLoader classLoader) { 213 return (T) createAopProxy().getProxy(classLoader); 214 } 215 216 } 217 | Popular Tags |