1 16 17 package org.springframework.aop.aspectj.annotation; 18 19 import java.util.LinkedList ; 20 import java.util.List ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.aspectj.lang.reflect.PerClauseKind; 25 26 import org.springframework.aop.Advisor; 27 import org.springframework.beans.factory.BeanFactoryUtils; 28 import org.springframework.beans.factory.ListableBeanFactory; 29 import org.springframework.util.Assert; 30 31 40 public class BeanFactoryAspectJAdvisorsBuilder { 41 42 private static final Log logger = LogFactory.getLog(BeanFactoryAspectJAdvisorsBuilder.class); 43 44 private final ListableBeanFactory beanFactory; 45 46 private final AspectJAdvisorFactory advisorFactory; 47 48 49 53 public BeanFactoryAspectJAdvisorsBuilder(ListableBeanFactory beanFactory) { 54 this(beanFactory, new ReflectiveAspectJAdvisorFactory()); 55 } 56 57 62 public BeanFactoryAspectJAdvisorsBuilder(ListableBeanFactory beanFactory, AspectJAdvisorFactory advisorFactory) { 63 Assert.notNull(beanFactory, "ListableBeanFactory must not be null"); 64 Assert.notNull(advisorFactory, "AspectJAdvisorFactory must not be null"); 65 this.beanFactory = beanFactory; 66 this.advisorFactory = advisorFactory; 67 } 68 69 70 77 public List <Advisor> buildAspectJAdvisors() { 78 List <Advisor> advisors = new LinkedList <Advisor>(); 79 String [] beanNames = 80 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Object .class, true, false); 81 82 for (String beanName : beanNames) { 83 if (!isEligibleBean(beanName)) { 84 continue; 85 } 86 87 Class beanType = this.beanFactory.getType(beanName); 91 if (beanType == null) { 92 continue; 93 } 94 95 if (this.advisorFactory.isAspect(beanType)) { 96 AspectMetadata amd = new AspectMetadata(beanType, beanName); 97 MetadataAwareAspectInstanceFactory factory = null; 98 if (amd.getAjType().getPerClause().getKind() == PerClauseKind.SINGLETON) { 99 factory = new BeanFactoryAspectInstanceFactory(this.beanFactory, beanName); 100 } 101 else { 102 if (this.beanFactory.isSingleton(beanName)) { 104 throw new IllegalArgumentException ( 105 "Bean with name '" + beanName + "' is a singleton, but aspect instantiation model is not singleton"); 106 } 107 factory = new PrototypeAspectInstanceFactory(this.beanFactory, beanName); 108 } 109 List <Advisor> classAdvisors = this.advisorFactory.getAdvisors(factory); 110 if (logger.isDebugEnabled()) { 111 logger.debug("Found " + classAdvisors.size() + 112 " AspectJ advice methods in bean with name '" + beanName + "'"); 113 } 114 advisors.addAll(classAdvisors); 115 } 116 } 117 return advisors; 118 } 119 120 124 protected boolean isEligibleBean(String beanName) { 125 return true; 126 } 127 128 } 129 | Popular Tags |