1 16 17 package org.springframework.aop.framework.autoproxy; 18 19 import java.util.Collections ; 20 import java.util.List ; 21 22 import org.springframework.aop.TargetSource; 23 import org.springframework.aop.support.AopUtils; 24 import org.springframework.beans.factory.BeanFactory; 25 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 26 import org.springframework.core.OrderComparator; 27 28 50 public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator { 51 52 private BeanFactoryAdvisorRetrievalHelper advisorRetrievalHelper; 53 54 55 public void setBeanFactory(BeanFactory beanFactory) { 56 super.setBeanFactory(beanFactory); 57 if (!(beanFactory instanceof ConfigurableListableBeanFactory)) { 58 throw new IllegalStateException ("Cannot use AdvisorAutoProxyCreator without a ConfigurableListableBeanFactory"); 59 } 60 initBeanFactory((ConfigurableListableBeanFactory) beanFactory); 61 } 62 63 protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) { 64 this.advisorRetrievalHelper = new BeanFactoryAdvisorRetrievalHelperAdapter(beanFactory); 65 } 66 67 68 protected Object [] getAdvicesAndAdvisorsForBean(Class beanClass, String name, TargetSource targetSource) { 69 List advisors = findEligibleAdvisors(beanClass); 70 if (advisors.isEmpty()) { 71 return DO_NOT_PROXY; 72 } 73 return advisors.toArray(); 74 } 75 76 84 protected List findEligibleAdvisors(Class clazz) { 85 List eligibleAdvisors = AopUtils.findAdvisorsThatCanApply(findCandidateAdvisors(), clazz); 86 if (!eligibleAdvisors.isEmpty()) { 87 eligibleAdvisors = sortAdvisors(eligibleAdvisors); 88 } 89 extendAdvisors(eligibleAdvisors); 90 return eligibleAdvisors; 91 } 92 93 97 protected List findCandidateAdvisors() { 98 return this.advisorRetrievalHelper.findAdvisorBeans(); 99 } 100 101 106 protected boolean isEligibleAdvisorBean(String beanName) { 107 return true; 108 } 109 110 116 protected List sortAdvisors(List advisors) { 117 Collections.sort(advisors, new OrderComparator()); 118 return advisors; 119 } 120 121 130 protected void extendAdvisors(List candidateAdvisors) { 131 } 132 133 134 138 private class BeanFactoryAdvisorRetrievalHelperAdapter extends BeanFactoryAdvisorRetrievalHelper { 139 140 public BeanFactoryAdvisorRetrievalHelperAdapter(ConfigurableListableBeanFactory beanFactory) { 141 super(beanFactory); 142 } 143 144 protected boolean isEligibleBean(String beanName) { 145 return AbstractAdvisorAutoProxyCreator.this.isEligibleAdvisorBean(beanName); 146 } 147 } 148 149 } 150 | Popular Tags |