1 16 17 package org.springframework.aop.aspectj.annotation; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.regex.Pattern ; 22 23 import org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator; 24 import org.springframework.beans.factory.ListableBeanFactory; 25 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 26 import org.springframework.util.Assert; 27 28 48 public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator { 49 50 private List <Pattern > includePatterns; 51 52 private AspectJAdvisorFactory aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory(); 53 54 private BeanFactoryAspectJAdvisorsBuilder aspectJAdvisorsBuilder; 55 56 57 61 public void setIncludePatterns(List <String > patterns) { 62 this.includePatterns = new ArrayList <Pattern >(patterns.size()); 63 for (String patternText : patterns) { 64 this.includePatterns.add(Pattern.compile(patternText)); 65 } 66 } 67 68 public void setAspectJAdvisorFactory(AspectJAdvisorFactory aspectJAdvisorFactory) { 69 Assert.notNull(this.aspectJAdvisorFactory, "AspectJAdvisorFactory must not be null"); 70 this.aspectJAdvisorFactory = aspectJAdvisorFactory; 71 } 72 73 @Override 74 protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) { 75 super.initBeanFactory(beanFactory); 76 this.aspectJAdvisorsBuilder = new BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory); 77 } 78 79 80 @Override 81 protected List findCandidateAdvisors() { 82 List advisors = super.findCandidateAdvisors(); 84 advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors()); 86 return advisors; 87 } 88 89 protected boolean isInfrastructureClass(Class beanClass) { 90 return (super.isInfrastructureClass(beanClass) || this.aspectJAdvisorFactory.isAspect(beanClass)); 99 } 100 101 107 protected boolean isEligibleAspectBean(String beanName) { 108 if (this.includePatterns == null) { 109 return true; 110 } 111 else { 112 for (Pattern pattern : this.includePatterns) { 113 if (pattern.matcher(beanName).matches()) { 114 return true; 115 } 116 } 117 return false; 118 } 119 } 120 121 122 126 private class BeanFactoryAspectJAdvisorsBuilderAdapter extends BeanFactoryAspectJAdvisorsBuilder { 127 128 public BeanFactoryAspectJAdvisorsBuilderAdapter( 129 ListableBeanFactory beanFactory, AspectJAdvisorFactory advisorFactory) { 130 super(beanFactory, advisorFactory); 131 } 132 133 protected boolean isEligibleBean(String beanName) { 134 return AnnotationAwareAspectJAutoProxyCreator.this.isEligibleAspectBean(beanName); 135 } 136 } 137 138 } 139 | Popular Tags |