KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > wiring > BeanConfigurerSupport


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.beans.factory.wiring;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.BeanFactoryAware;
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
27 import org.springframework.util.Assert;
28
29 /**
30  * Convenient superclass for configurers that can perform Dependency Injection
31  * on objects (however they may be created).
32  *
33  * <p>Typically subclassed by AspectJ aspects.
34
35  * <p>Subclasses may also need a metadata resolution strategy, in the
36  * {@link BeanWiringInfoResolver} interface. The default implementation looks
37  * for a bean with the same name as the fully-qualified class name. (This is
38  * the default name of the bean in a Spring XML file if the '<code>id</code>'
39  * attribute is not used.)
40
41  * @author Rob Harrop
42  * @author Rod Johnson
43  * @author Juergen Hoeller
44  * @author Adrian Colyer
45  * @since 2.0
46  */

47 public abstract class BeanConfigurerSupport implements BeanFactoryAware, InitializingBean, DisposableBean {
48
49     /** Logger available to subclasses */
50     protected Log logger = LogFactory.getLog(getClass());
51
52     private BeanWiringInfoResolver beanWiringInfoResolver;
53
54     private AutowireCapableBeanFactory beanFactory;
55
56
57     /**
58      * Set the <code>BeanWiringInfoResolver</code> to use.
59      * <p>Default behavior will be to look for a bean with the same name as the
60      * class.
61      * <p>As an alternative, consider using annotation-driven bean wiring.
62      * @param beanWiringInfoResolver the <code>BeanWiringInfoResolver</code> to use.
63      * @see ClassNameBeanWiringInfoResolver
64      * @see org.springframework.beans.factory.annotation.AnnotationBeanWiringInfoResolver
65      */

66     public void setBeanWiringInfoResolver(BeanWiringInfoResolver beanWiringInfoResolver) {
67         Assert.notNull(beanWiringInfoResolver, "'beanWiringInfoResolver' cannot be null.");
68         this.beanWiringInfoResolver = beanWiringInfoResolver;
69     }
70
71     /**
72      * Set the {@link BeanFactory} in which this aspect must configure beans.
73      * @throws IllegalArgumentException if the supplied <code>beanFactory</code> is
74      * not an {@link AutowireCapableBeanFactory}.
75      */

76     public void setBeanFactory(BeanFactory beanFactory) {
77         if (!(beanFactory instanceof AutowireCapableBeanFactory)) {
78             throw new IllegalArgumentException JavaDoc(
79                  "Bean configurer aspect needs to run in an AutowireCapableBeanFactory, not in [" + beanFactory + "]");
80         }
81         this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
82     }
83
84     /**
85      * If no {@link #setBeanWiringInfoResolver BeanWiringInfoResolver} was
86      * provided, use a {@link ClassNameBeanWiringInfoResolver} as the default.
87      */

88     public void afterPropertiesSet() throws Exception JavaDoc {
89         if (this.beanWiringInfoResolver == null) {
90             this.beanWiringInfoResolver = new ClassNameBeanWiringInfoResolver();
91         }
92     }
93     
94     /**
95      * Release references to the {@link BeanFactory} and
96      * {@link BeanWiringInfoResolver} when the container is destroyed.
97      */

98     public void destroy() {
99         this.beanFactory = null;
100         this.beanWiringInfoResolver = null;
101     }
102
103
104     /**
105      * Configure the bean instance.
106      * <p>Subclasses can override this to provide custom configuration logic.
107      * Typically called by an aspect, for all bean instances matched by a
108      * pointcut.
109      * @param beanInstance the bean instance to configure (must <b>not</b> be <code>null</code>)
110      */

111     protected void configureBean(Object JavaDoc beanInstance) {
112         if (this.beanWiringInfoResolver == null) {
113             if(logger.isWarnEnabled()) {
114                 logger.warn("[" + getClass().getName() + "] has not been configured by Spring " +
115                     "and is unable to configure bean instances. Object with identity " +
116                     "hashcode " + System.identityHashCode(beanInstance) + " has not been configured: " +
117                     "Make sure this configurer runs in a Spring container. " +
118                     "For example, add it to a Spring application context as an XML bean definition.");
119             }
120             return;
121         }
122         
123         BeanWiringInfo bwi = this.beanWiringInfoResolver.resolveWiringInfo(beanInstance);
124         if (bwi == null) {
125             // Skip the bean if no wiring info given.
126
return;
127         }
128
129         if (this.beanFactory == null) {
130             if(logger.isWarnEnabled()) {
131                 logger.warn("BeanFactory has not been set on [" + getClass().getName() + "]: " +
132                     "Make sure this configurer runs in a Spring container. " +
133                     "For example, add it to a Spring application context as an XML bean definition.");
134             }
135             return;
136         }
137
138         if (bwi.indicatesAutowiring()) {
139             // Perform autowiring.
140
this.beanFactory.autowireBeanProperties(beanInstance, bwi.getAutowireMode(), bwi.getDependencyCheck());
141         }
142         else {
143             // Perform explicit wiring.
144
this.beanFactory.applyBeanPropertyValues(beanInstance, bwi.getBeanName());
145         }
146     }
147
148 }
149
Popular Tags