KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > aspectj > annotation > AnnotationAwareAspectJAutoProxyCreator


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.aspectj.annotation;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.regex.Pattern JavaDoc;
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 /**
29  * AspectJAwareAdvisorAutoProxyCreator subclass that processes all AspectJ
30  * annotation aspects in the current application context, as well as Spring Advisors.
31  *
32  * <p>Any AspectJ annotated classes will automatically be recognized, and their
33  * advice applied if Spring AOP's proxy-based model is capable of applying it.
34  * This covers method execution joinpoints.
35  *
36  * <p>If the &lt;aop:include&gt; element is used, only @AspectJ beans with names matched by
37  * an include pattern will be considered as defining aspects to use for Spring auto-proxying.
38  *
39  * <p>Processing of Spring Advisors follows the rules established in
40  * {@link org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator}.
41  *
42  * @author Rod Johnson
43  * @author Juergen Hoeller
44  * @since 2.0
45  * @see org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator
46  * @see org.springframework.aop.aspectj.annotation.AspectJAdvisorFactory
47  */

48 public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator {
49
50     private List JavaDoc<Pattern JavaDoc> includePatterns;
51
52     private AspectJAdvisorFactory aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory();
53
54     private BeanFactoryAspectJAdvisorsBuilder aspectJAdvisorsBuilder;
55
56
57     /**
58      * Set a list of regex patterns, matching eligible @AspectJ bean names.
59      * <p>Default is to consider all @AspectJ beans as eligible.
60      */

61     public void setIncludePatterns(List JavaDoc<String JavaDoc> patterns) {
62         this.includePatterns = new ArrayList JavaDoc<Pattern JavaDoc>(patterns.size());
63         for (String JavaDoc 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 JavaDoc
74     protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
75         super.initBeanFactory(beanFactory);
76         this.aspectJAdvisorsBuilder = new BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory);
77     }
78
79
80     @Override JavaDoc
81     protected List JavaDoc findCandidateAdvisors() {
82         // Add all the Spring advisors found according to superclass rules.
83
List JavaDoc advisors = super.findCandidateAdvisors();
84         // Build Advisors for all AspectJ aspects in the bean factory.
85
advisors.addAll(this.aspectJAdvisorsBuilder.buildAspectJAdvisors());
86         return advisors;
87     }
88
89     protected boolean isInfrastructureClass(Class JavaDoc beanClass) {
90         // Previously we setProxyTargetClass(true) in the constructor, but that has too
91
// broad an impact. Instead we now override isInfrastructureClass to avoid proxying
92
// aspects. I'm not entirely happy with that as there is no good reason not
93
// to advise aspects, except that it causes advice invocation to go through a
94
// proxy, and if the aspect implements e.g the Ordered interface it will be
95
// proxied by that interface and fail at runtime as the advice method is not
96
// defined on the interface. We could potentially relax the restriction about
97
// not advising aspects in the future.
98
return (super.isInfrastructureClass(beanClass) || this.aspectJAdvisorFactory.isAspect(beanClass));
99     }
100
101     /**
102      * Check whether the given aspect bean is eligible for auto-proxying.
103      * <p>If no &lt;aop:include&gt; elements were used then "includePatterns" will be
104      * <code>null</code> and all beans are included. If "includePatterns" is non-null,
105      * then one of the patterns must match.
106      */

107     protected boolean isEligibleAspectBean(String JavaDoc beanName) {
108         if (this.includePatterns == null) {
109             return true;
110         }
111         else {
112             for (Pattern JavaDoc pattern : this.includePatterns) {
113                 if (pattern.matcher(beanName).matches()) {
114                     return true;
115                 }
116             }
117             return false;
118         }
119     }
120
121
122     /**
123      * Subclass of BeanFactoryAspectJAdvisorsBuilderAdapter that delegates to
124      * surrounding AnnotationAwareAspectJAutoProxyCreator facilities.
125      */

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 JavaDoc beanName) {
134             return AnnotationAwareAspectJAutoProxyCreator.this.isEligibleAspectBean(beanName);
135         }
136     }
137
138 }
139
Popular Tags