KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > BeanFactoryAdvisorRetrievalHelper


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.framework.autoproxy;
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
25 import org.springframework.aop.Advisor;
26 import org.springframework.beans.factory.BeanCreationException;
27 import org.springframework.beans.factory.BeanCurrentlyInCreationException;
28 import org.springframework.beans.factory.BeanFactoryUtils;
29 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
30 import org.springframework.util.Assert;
31
32 /**
33  * Helper for retrieving standard Spring Advisors from a BeanFactory,
34  * for use with auto-proxying.
35  *
36  * @author Juergen Hoeller
37  * @since 2.0.2
38  * @see AbstractAdvisorAutoProxyCreator
39  */

40 public class BeanFactoryAdvisorRetrievalHelper {
41
42     private static final Log logger = LogFactory.getLog(BeanFactoryAdvisorRetrievalHelper.class);
43
44     private final ConfigurableListableBeanFactory beanFactory;
45
46
47     /**
48      * Create a new BeanFactoryAdvisorRetrievalHelper for the given BeanFactory.
49      * @param beanFactory the ListableBeanFactory to scan
50      */

51     public BeanFactoryAdvisorRetrievalHelper(ConfigurableListableBeanFactory beanFactory) {
52         Assert.notNull(beanFactory, "ListableBeanFactory must not be null");
53         this.beanFactory = beanFactory;
54     }
55
56
57     /**
58      * Find all eligible Advisor beans in the current bean factory,
59      * ignoring FactoryBeans and excluding beans that are currently in creation.
60      * @return the list of {@link org.springframework.aop.Advisor} beans
61      * @see #isEligibleBean
62      */

63     public List JavaDoc findAdvisorBeans() {
64         List JavaDoc advisors = new LinkedList JavaDoc();
65         // Do not initialize FactoryBeans here: We need to leave all regular beans
66
// uninitialized to let the auto-proxy creator apply to them!
67
String JavaDoc[] advisorNames =
68                 BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, Advisor.class, true, false);
69         for (int i = 0; i < advisorNames.length; i++) {
70             String JavaDoc name = advisorNames[i];
71             if (isEligibleBean(name) && !this.beanFactory.isCurrentlyInCreation(name)) {
72                 try {
73                     advisors.add(this.beanFactory.getBean(name));
74                 }
75                 catch (BeanCreationException ex) {
76                     Throwable JavaDoc rootCause = ex.getMostSpecificCause();
77                     if (rootCause instanceof BeanCurrentlyInCreationException) {
78                         BeanCreationException bce = (BeanCreationException) rootCause;
79                         if (this.beanFactory.isCurrentlyInCreation(bce.getBeanName())) {
80                             if (logger.isDebugEnabled()) {
81                                 logger.debug("Ignoring currently created advisor '" + name + "': " + ex.getMessage());
82                             }
83                             // Ignore: indicates a reference back to the bean we're trying to advise.
84
// We want to find advisors other than the currently created bean itself.
85
continue;
86                         }
87                     }
88                     throw ex;
89                 }
90             }
91         }
92         return advisors;
93     }
94
95     /**
96      * Determine whether the aspect bean with the given name is eligible.
97      * <p>The default implementation always returns <code>true</code>.
98      * @param beanName the name of the aspect bean
99      * @return whether the bean is eligible
100      */

101     protected boolean isEligibleBean(String JavaDoc beanName) {
102         return true;
103     }
104
105 }
106
Popular Tags