001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.support;
018:
019: import java.util.Locale;
020:
021: import org.springframework.beans.BeansException;
022: import org.springframework.beans.MutablePropertyValues;
023: import org.springframework.beans.factory.support.RootBeanDefinition;
024: import org.springframework.context.ApplicationContext;
025:
026: /**
027: * {@link org.springframework.context.ApplicationContext} implementation
028: * which supports programmatic registration of beans and messages,
029: * rather than reading bean definitions from external configuration sources.
030: * Mainly useful for testing.
031: *
032: * @author Rod Johnson
033: * @author Juergen Hoeller
034: * @see #registerSingleton
035: * @see #registerPrototype
036: * @see #registerBeanDefinition
037: * @see #refresh
038: */
039: public class StaticApplicationContext extends GenericApplicationContext {
040:
041: private final StaticMessageSource staticMessageSource;
042:
043: /**
044: * Create a new StaticApplicationContext.
045: * @see #registerSingleton
046: * @see #registerPrototype
047: * @see #registerBeanDefinition
048: * @see #refresh
049: */
050: public StaticApplicationContext() throws BeansException {
051: this (null);
052: }
053:
054: /**
055: * Create a new StaticApplicationContext with the given parent.
056: * @see #registerSingleton
057: * @see #registerPrototype
058: * @see #registerBeanDefinition
059: * @see #refresh
060: */
061: public StaticApplicationContext(ApplicationContext parent)
062: throws BeansException {
063: super (parent);
064:
065: // Initialize and register a StaticMessageSource.
066: this .staticMessageSource = new StaticMessageSource();
067: getBeanFactory().registerSingleton(MESSAGE_SOURCE_BEAN_NAME,
068: this .staticMessageSource);
069: }
070:
071: /**
072: * Return the internal StaticMessageSource used by this context.
073: * Can be used to register messages on it.
074: * @see #addMessage
075: */
076: public StaticMessageSource getStaticMessageSource() {
077: return this .staticMessageSource;
078: }
079:
080: /**
081: * Register a singleton bean with the underlying bean factory.
082: * <p>For more advanced needs, register with the underlying BeanFactory directly.
083: * @see #getDefaultListableBeanFactory
084: */
085: public void registerSingleton(String name, Class clazz)
086: throws BeansException {
087: getDefaultListableBeanFactory().registerBeanDefinition(name,
088: new RootBeanDefinition(clazz));
089: }
090:
091: /**
092: * Register a singleton bean with the underlying bean factory.
093: * <p>For more advanced needs, register with the underlying BeanFactory directly.
094: * @see #getDefaultListableBeanFactory
095: */
096: public void registerSingleton(String name, Class clazz,
097: MutablePropertyValues pvs) throws BeansException {
098: getDefaultListableBeanFactory().registerBeanDefinition(name,
099: new RootBeanDefinition(clazz, pvs));
100: }
101:
102: /**
103: * Register a prototype bean with the underlying bean factory.
104: * <p>For more advanced needs, register with the underlying BeanFactory directly.
105: * @see #getDefaultListableBeanFactory
106: */
107: public void registerPrototype(String name, Class clazz)
108: throws BeansException {
109: getDefaultListableBeanFactory().registerBeanDefinition(name,
110: new RootBeanDefinition(clazz, false));
111: }
112:
113: /**
114: * Register a prototype bean with the underlying bean factory.
115: * <p>For more advanced needs, register with the underlying BeanFactory directly.
116: * @see #getDefaultListableBeanFactory
117: */
118: public void registerPrototype(String name, Class clazz,
119: MutablePropertyValues pvs) throws BeansException {
120: getDefaultListableBeanFactory().registerBeanDefinition(name,
121: new RootBeanDefinition(clazz, pvs, false));
122: }
123:
124: /**
125: * Associate the given message with the given code.
126: * @param code lookup code
127: * @param locale locale message should be found within
128: * @param defaultMessage message associated with this lookup code
129: * @see #getStaticMessageSource
130: */
131: public void addMessage(String code, Locale locale,
132: String defaultMessage) {
133: getStaticMessageSource().addMessage(code, locale,
134: defaultMessage);
135: }
136:
137: }
|