KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > generic > GenericBeanFactoryAccessor


1 /*
2  * Copyright 2002-2006 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.generic;
18
19 import java.lang.annotation.Annotation JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.springframework.beans.BeansException;
24 import org.springframework.beans.factory.ListableBeanFactory;
25 import org.springframework.util.Assert;
26
27 /**
28  * Simple wrapper around a {@link ListableBeanFactory} that provides typed, generics-based
29  * access to key methods. This removes the need for casting in many cases and should
30  * increase compile-time type safety.
31  *
32  * <p>Provides a simple mechanism for accessing all beans with a particular {@link Annotation}.
33  *
34  * @author Rob Harrop
35  * @author Juergen Hoeller
36  * @since 2.0
37  */

38 public class GenericBeanFactoryAccessor {
39
40     /**
41      * The {@link ListableBeanFactory} being wrapped.
42      */

43     private final ListableBeanFactory beanFactory;
44
45
46     /**
47      * Constructs a <code>GenericBeanFactoryAccessor</code> that wraps the supplied {@link ListableBeanFactory}.
48      */

49     public GenericBeanFactoryAccessor(ListableBeanFactory beanFactory) {
50         Assert.notNull(beanFactory, "Bean factory must not be null");
51         this.beanFactory = beanFactory;
52     }
53
54     /**
55      * Return the wrapped {@link ListableBeanFactory}.
56      */

57     public final ListableBeanFactory getBeanFactory() {
58         return this.beanFactory;
59     }
60
61
62     /**
63      * @see org.springframework.beans.factory.BeanFactory#getBean(String)
64      */

65     public <T> T getBean(String JavaDoc name) throws BeansException {
66         return (T) getBeanFactory().getBean(name);
67     }
68
69     /**
70      * @see org.springframework.beans.factory.BeanFactory#getBean(String, Class)
71      */

72     public <T> T getBean(String JavaDoc name, Class JavaDoc<T> requiredType) throws BeansException {
73         return (T) getBeanFactory().getBean(name, requiredType);
74     }
75
76     /**
77      * @see ListableBeanFactory#getBeansOfType(Class)
78      */

79     public <T> Map JavaDoc<String JavaDoc, T> getBeansOfType(Class JavaDoc<T> type) throws BeansException {
80         return getBeanFactory().getBeansOfType(type);
81     }
82
83     /**
84      * @see ListableBeanFactory#getBeansOfType(Class, boolean, boolean)
85      */

86     public <T> Map JavaDoc<String JavaDoc, T> getBeansOfType(Class JavaDoc<T> type, boolean includePrototypes, boolean allowEagerInit)
87             throws BeansException {
88
89         return getBeanFactory().getBeansOfType(type, includePrototypes, allowEagerInit);
90     }
91
92     /**
93      * Find all beans whose <code>Class</code> has the supplied {@link Annotation} type.
94      * @param annotationType the type of annotation to look for
95      * @return a Map with the matching beans, containing the bean names as
96      * keys and the corresponding bean instances as values
97      */

98     public Map JavaDoc<String JavaDoc, Object JavaDoc> getBeansWithAnnotation(Class JavaDoc<? extends Annotation JavaDoc> annotationType) {
99         Map JavaDoc<String JavaDoc, Object JavaDoc> results = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
100         for (String JavaDoc beanName : getBeanFactory().getBeanNamesForType(null)) {
101             Class JavaDoc beanType = getBeanFactory().getType(beanName);
102             if (beanType.getAnnotation(annotationType) != null) {
103                 results.put(beanName, getBeanFactory().getBean(beanName));
104             }
105         }
106         return results;
107     }
108
109 }
110
Popular Tags