KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > StaticApplicationContext


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.context.support;
18
19 import java.util.Locale JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.MutablePropertyValues;
23 import org.springframework.beans.factory.support.RootBeanDefinition;
24 import org.springframework.context.ApplicationContext;
25
26 /**
27  * {@link org.springframework.context.ApplicationContext} implementation
28  * which supports programmatic registration of beans and messages,
29  * rather than reading bean definitions from external configuration sources.
30  * Mainly useful for testing.
31  *
32  * @author Rod Johnson
33  * @author Juergen Hoeller
34  * @see #registerSingleton
35  * @see #registerPrototype
36  * @see #registerBeanDefinition
37  * @see #refresh
38  */

39 public class StaticApplicationContext extends GenericApplicationContext {
40
41     private final StaticMessageSource staticMessageSource;
42
43
44     /**
45      * Create a new StaticApplicationContext.
46      * @see #registerSingleton
47      * @see #registerPrototype
48      * @see #registerBeanDefinition
49      * @see #refresh
50      */

51     public StaticApplicationContext() throws BeansException {
52         this(null);
53     }
54
55     /**
56      * Create a new StaticApplicationContext with the given parent.
57      * @see #registerSingleton
58      * @see #registerPrototype
59      * @see #registerBeanDefinition
60      * @see #refresh
61      */

62     public StaticApplicationContext(ApplicationContext parent) throws BeansException {
63         super(parent);
64
65         // Initialize and register a StaticMessageSource.
66
this.staticMessageSource = new StaticMessageSource();
67         getBeanFactory().registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.staticMessageSource);
68     }
69
70     /**
71      * Return the internal StaticMessageSource used by this context.
72      * Can be used to register messages on it.
73      * @see #addMessage
74      */

75     public StaticMessageSource getStaticMessageSource() {
76         return this.staticMessageSource;
77     }
78
79
80     /**
81      * Register a singleton bean with the underlying bean factory.
82      * <p>For more advanced needs, register with the underlying BeanFactory directly.
83      * @see #getDefaultListableBeanFactory
84      */

85     public void registerSingleton(String JavaDoc name, Class JavaDoc clazz) throws BeansException {
86         getDefaultListableBeanFactory().registerBeanDefinition(name, new RootBeanDefinition(clazz));
87     }
88
89     /**
90      * Register a singleton bean with the underlying bean factory.
91      * <p>For more advanced needs, register with the underlying BeanFactory directly.
92      * @see #getDefaultListableBeanFactory
93      */

94     public void registerSingleton(String JavaDoc name, Class JavaDoc clazz, MutablePropertyValues pvs) throws BeansException {
95         getDefaultListableBeanFactory().registerBeanDefinition(name, new RootBeanDefinition(clazz, pvs));
96     }
97
98     /**
99      * Register a prototype bean with the underlying bean factory.
100      * <p>For more advanced needs, register with the underlying BeanFactory directly.
101      * @see #getDefaultListableBeanFactory
102      */

103     public void registerPrototype(String JavaDoc name, Class JavaDoc clazz) throws BeansException {
104         getDefaultListableBeanFactory().registerBeanDefinition(name, new RootBeanDefinition(clazz, false));
105     }
106
107     /**
108      * Register a prototype bean with the underlying bean factory.
109      * <p>For more advanced needs, register with the underlying BeanFactory directly.
110      * @see #getDefaultListableBeanFactory
111      */

112     public void registerPrototype(String JavaDoc name, Class JavaDoc clazz, MutablePropertyValues pvs) throws BeansException {
113         getDefaultListableBeanFactory().registerBeanDefinition(name, new RootBeanDefinition(clazz, pvs, false));
114     }
115
116     /**
117      * Associate the given message with the given code.
118      * @param code lookup code
119      * @param locale locale message should be found within
120      * @param defaultMessage message associated with this lookup code
121      * @see #getStaticMessageSource
122      */

123     public void addMessage(String JavaDoc code, Locale JavaDoc locale, String JavaDoc defaultMessage) {
124         getStaticMessageSource().addMessage(code, locale, defaultMessage);
125     }
126
127 }
128
Popular Tags