/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.context.support;

import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;

ApplicationContext implementation which supports programmatic registration of beans and messages, rather than reading bean definitions from external configuration sources. Mainly useful for testing.
Author:Rod Johnson, Juergen Hoeller
See Also:
/** * {@link org.springframework.context.ApplicationContext} implementation * which supports programmatic registration of beans and messages, * rather than reading bean definitions from external configuration sources. * Mainly useful for testing. * * @author Rod Johnson * @author Juergen Hoeller * @see #registerSingleton * @see #registerPrototype * @see #registerBeanDefinition * @see #refresh */
public class StaticApplicationContext extends GenericApplicationContext { private final StaticMessageSource staticMessageSource;
Create a new StaticApplicationContext.
See Also:
/** * Create a new StaticApplicationContext. * @see #registerSingleton * @see #registerPrototype * @see #registerBeanDefinition * @see #refresh */
public StaticApplicationContext() throws BeansException { this(null); }
Create a new StaticApplicationContext with the given parent.
See Also:
/** * Create a new StaticApplicationContext with the given parent. * @see #registerSingleton * @see #registerPrototype * @see #registerBeanDefinition * @see #refresh */
public StaticApplicationContext(@Nullable ApplicationContext parent) throws BeansException { super(parent); // Initialize and register a StaticMessageSource. this.staticMessageSource = new StaticMessageSource(); getBeanFactory().registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.staticMessageSource); }
Overridden to turn it into a no-op, to be more lenient towards test cases.
/** * Overridden to turn it into a no-op, to be more lenient towards test cases. */
@Override protected void assertBeanFactoryActive() { }
Return the internal StaticMessageSource used by this context. Can be used to register messages on it.
See Also:
  • addMessage
/** * Return the internal StaticMessageSource used by this context. * Can be used to register messages on it. * @see #addMessage */
public final StaticMessageSource getStaticMessageSource() { return this.staticMessageSource; }
Register a singleton bean with the underlying bean factory.

For more advanced needs, register with the underlying BeanFactory directly.

See Also:
  • getDefaultListableBeanFactory
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */
public void registerSingleton(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
Register a singleton bean with the underlying bean factory.

For more advanced needs, register with the underlying BeanFactory directly.

See Also:
  • getDefaultListableBeanFactory
/** * Register a singleton bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */
public void registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
Register a prototype bean with the underlying bean factory.

For more advanced needs, register with the underlying BeanFactory directly.

See Also:
  • getDefaultListableBeanFactory
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */
public void registerPrototype(String name, Class<?> clazz) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
Register a prototype bean with the underlying bean factory.

For more advanced needs, register with the underlying BeanFactory directly.

See Also:
  • getDefaultListableBeanFactory
/** * Register a prototype bean with the underlying bean factory. * <p>For more advanced needs, register with the underlying BeanFactory directly. * @see #getDefaultListableBeanFactory */
public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { GenericBeanDefinition bd = new GenericBeanDefinition(); bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setBeanClass(clazz); bd.setPropertyValues(pvs); getDefaultListableBeanFactory().registerBeanDefinition(name, bd); }
Associate the given message with the given code.
Params:
  • code – lookup code
  • locale – the locale message should be found within
  • defaultMessage – message associated with this lookup code
See Also:
/** * Associate the given message with the given code. * @param code lookup code * @param locale the locale message should be found within * @param defaultMessage message associated with this lookup code * @see #getStaticMessageSource */
public void addMessage(String code, Locale locale, String defaultMessage) { getStaticMessageSource().addMessage(code, locale, defaultMessage); } }