001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.aop.aspectj.annotation; 018 019import java.util.List; 020import java.util.Map; 021import java.util.concurrent.ConcurrentHashMap; 022 023import org.aspectj.lang.reflect.PerClauseKind; 024 025import org.springframework.aop.Advisor; 026import org.springframework.aop.aspectj.AspectJProxyUtils; 027import org.springframework.aop.aspectj.SimpleAspectInstanceFactory; 028import org.springframework.aop.framework.ProxyCreatorSupport; 029import org.springframework.aop.support.AopUtils; 030import org.springframework.core.annotation.AnnotationAwareOrderComparator; 031import org.springframework.util.Assert; 032import org.springframework.util.ClassUtils; 033 034/** 035 * AspectJ-based proxy factory, allowing for programmatic building 036 * of proxies which include AspectJ aspects (code style as well 037 * Java 5 annotation style). 038 * 039 * @author Rob Harrop 040 * @author Juergen Hoeller 041 * @author Ramnivas Laddad 042 * @since 2.0 043 * @see #addAspect(Object) 044 * @see #addAspect(Class) 045 * @see #getProxy() 046 * @see #getProxy(ClassLoader) 047 * @see org.springframework.aop.framework.ProxyFactory 048 */ 049@SuppressWarnings("serial") 050public class AspectJProxyFactory extends ProxyCreatorSupport { 051 052 /** Cache for singleton aspect instances. */ 053 private static final Map<Class<?>, Object> aspectCache = new ConcurrentHashMap<>(); 054 055 private final AspectJAdvisorFactory aspectFactory = new ReflectiveAspectJAdvisorFactory(); 056 057 058 /** 059 * Create a new AspectJProxyFactory. 060 */ 061 public AspectJProxyFactory() { 062 } 063 064 /** 065 * Create a new AspectJProxyFactory. 066 * <p>Will proxy all interfaces that the given target implements. 067 * @param target the target object to be proxied 068 */ 069 public AspectJProxyFactory(Object target) { 070 Assert.notNull(target, "Target object must not be null"); 071 setInterfaces(ClassUtils.getAllInterfaces(target)); 072 setTarget(target); 073 } 074 075 /** 076 * Create a new {@code AspectJProxyFactory}. 077 * No target, only interfaces. Must add interceptors. 078 */ 079 public AspectJProxyFactory(Class<?>... interfaces) { 080 setInterfaces(interfaces); 081 } 082 083 084 /** 085 * Add the supplied aspect instance to the chain. The type of the aspect instance 086 * supplied must be a singleton aspect. True singleton lifecycle is not honoured when 087 * using this method - the caller is responsible for managing the lifecycle of any 088 * aspects added in this way. 089 * @param aspectInstance the AspectJ aspect instance 090 */ 091 public void addAspect(Object aspectInstance) { 092 Class<?> aspectClass = aspectInstance.getClass(); 093 String aspectName = aspectClass.getName(); 094 AspectMetadata am = createAspectMetadata(aspectClass, aspectName); 095 if (am.getAjType().getPerClause().getKind() != PerClauseKind.SINGLETON) { 096 throw new IllegalArgumentException( 097 "Aspect class [" + aspectClass.getName() + "] does not define a singleton aspect"); 098 } 099 addAdvisorsFromAspectInstanceFactory( 100 new SingletonMetadataAwareAspectInstanceFactory(aspectInstance, aspectName)); 101 } 102 103 /** 104 * Add an aspect of the supplied type to the end of the advice chain. 105 * @param aspectClass the AspectJ aspect class 106 */ 107 public void addAspect(Class<?> aspectClass) { 108 String aspectName = aspectClass.getName(); 109 AspectMetadata am = createAspectMetadata(aspectClass, aspectName); 110 MetadataAwareAspectInstanceFactory instanceFactory = createAspectInstanceFactory(am, aspectClass, aspectName); 111 addAdvisorsFromAspectInstanceFactory(instanceFactory); 112 } 113 114 115 /** 116 * Add all {@link Advisor Advisors} from the supplied {@link MetadataAwareAspectInstanceFactory} 117 * to the current chain. Exposes any special purpose {@link Advisor Advisors} if needed. 118 * @see AspectJProxyUtils#makeAdvisorChainAspectJCapableIfNecessary(List) 119 */ 120 private void addAdvisorsFromAspectInstanceFactory(MetadataAwareAspectInstanceFactory instanceFactory) { 121 List<Advisor> advisors = this.aspectFactory.getAdvisors(instanceFactory); 122 Class<?> targetClass = getTargetClass(); 123 Assert.state(targetClass != null, "Unresolvable target class"); 124 advisors = AopUtils.findAdvisorsThatCanApply(advisors, targetClass); 125 AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(advisors); 126 AnnotationAwareOrderComparator.sort(advisors); 127 addAdvisors(advisors); 128 } 129 130 /** 131 * Create an {@link AspectMetadata} instance for the supplied aspect type. 132 */ 133 private AspectMetadata createAspectMetadata(Class<?> aspectClass, String aspectName) { 134 AspectMetadata am = new AspectMetadata(aspectClass, aspectName); 135 if (!am.getAjType().isAspect()) { 136 throw new IllegalArgumentException("Class [" + aspectClass.getName() + "] is not a valid aspect type"); 137 } 138 return am; 139 } 140 141 /** 142 * Create a {@link MetadataAwareAspectInstanceFactory} for the supplied aspect type. If the aspect type 143 * has no per clause, then a {@link SingletonMetadataAwareAspectInstanceFactory} is returned, otherwise 144 * a {@link PrototypeAspectInstanceFactory} is returned. 145 */ 146 private MetadataAwareAspectInstanceFactory createAspectInstanceFactory( 147 AspectMetadata am, Class<?> aspectClass, String aspectName) { 148 149 MetadataAwareAspectInstanceFactory instanceFactory; 150 if (am.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { 151 // Create a shared aspect instance. 152 Object instance = getSingletonAspectInstance(aspectClass); 153 instanceFactory = new SingletonMetadataAwareAspectInstanceFactory(instance, aspectName); 154 } 155 else { 156 // Create a factory for independent aspect instances. 157 instanceFactory = new SimpleMetadataAwareAspectInstanceFactory(aspectClass, aspectName); 158 } 159 return instanceFactory; 160 } 161 162 /** 163 * Get the singleton aspect instance for the supplied aspect type. An instance 164 * is created if one cannot be found in the instance cache. 165 */ 166 private Object getSingletonAspectInstance(Class<?> aspectClass) { 167 // Quick check without a lock... 168 Object instance = aspectCache.get(aspectClass); 169 if (instance == null) { 170 synchronized (aspectCache) { 171 // To be safe, check within full lock now... 172 instance = aspectCache.get(aspectClass); 173 if (instance == null) { 174 instance = new SimpleAspectInstanceFactory(aspectClass).getAspectInstance(); 175 aspectCache.put(aspectClass, instance); 176 } 177 } 178 } 179 return instance; 180 } 181 182 183 /** 184 * Create a new proxy according to the settings in this factory. 185 * <p>Can be called repeatedly. Effect will vary if we've added 186 * or removed interfaces. Can add and remove interceptors. 187 * <p>Uses a default class loader: Usually, the thread context class loader 188 * (if necessary for proxy creation). 189 * @return the new proxy 190 */ 191 @SuppressWarnings("unchecked") 192 public <T> T getProxy() { 193 return (T) createAopProxy().getProxy(); 194 } 195 196 /** 197 * Create a new proxy according to the settings in this factory. 198 * <p>Can be called repeatedly. Effect will vary if we've added 199 * or removed interfaces. Can add and remove interceptors. 200 * <p>Uses the given class loader (if necessary for proxy creation). 201 * @param classLoader the class loader to create the proxy with 202 * @return the new proxy 203 */ 204 @SuppressWarnings("unchecked") 205 public <T> T getProxy(ClassLoader classLoader) { 206 return (T) createAopProxy().getProxy(classLoader); 207 } 208 209}