KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
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 /**
32  * Helper for retrieving @AspectJ beans from a BeanFactory and building
33  * Spring Advisors based on them, for use with auto-proxying.
34  *
35  * @author Juergen Hoeller
36  * @since 2.0.2
37  * @see AnnotationAwareAspectJAutoProxyCreator
38  * @see org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper
39  */

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     /**
50      * Create a new BeanFactoryAspectJAdvisorsBuilder for the given BeanFactory.
51      * @param beanFactory the ListableBeanFactory to scan
52      */

53     public BeanFactoryAspectJAdvisorsBuilder(ListableBeanFactory beanFactory) {
54         this(beanFactory, new ReflectiveAspectJAdvisorFactory());
55     }
56
57     /**
58      * Create a new BeanFactoryAspectJAdvisorsBuilder for the given BeanFactory.
59      * @param beanFactory the ListableBeanFactory to scan
60      * @param advisorFactory the AspectJAdvisorFactory to build each Advisor with
61      */

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     /**
71      * Look for AspectJ-annotated aspect beans in the current bean factory,
72      * and return to a list of Spring AOP Advisors representing them.
73      * <p>Creates a Spring Advisor for each AspectJ advice method.
74      * @return the list of {@link org.springframework.aop.Advisor} beans
75      * @see #isEligibleBean
76      */

77     public List JavaDoc<Advisor> buildAspectJAdvisors() {
78         List JavaDoc<Advisor> advisors = new LinkedList JavaDoc<Advisor>();
79         String JavaDoc[] beanNames =
80                 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Object JavaDoc.class, true, false);
81
82         for (String JavaDoc beanName : beanNames) {
83             if (!isEligibleBean(beanName)) {
84                 continue;
85             }
86
87             // We must be careful not to instantiate beans eagerly as in this
88
// case they would be cached by the Spring container but would not
89
// have been weaved
90
Class JavaDoc 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                     // Per target or per this
103
if (this.beanFactory.isSingleton(beanName)) {
104                         throw new IllegalArgumentException JavaDoc(
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 JavaDoc<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     /**
121      * Return whether the aspect bean with the given name is eligible.
122      * @param beanName the name of the aspect bean
123      */

124     protected boolean isEligibleBean(String JavaDoc beanName) {
125         return true;
126     }
127
128 }
129
Popular Tags