1. /*
  2. * Copyright 2002-2004 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. package org.springframework.context.support;
  17. import java.util.Locale;
  18. import org.springframework.beans.BeansException;
  19. import org.springframework.beans.MutablePropertyValues;
  20. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  21. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  22. import org.springframework.beans.factory.support.RootBeanDefinition;
  23. import org.springframework.context.ApplicationContext;
  24. /**
  25. * ApplicationContext that allows concrete registration of beans and
  26. * messages in code, rather than from external configuration sources.
  27. * Mainly useful for testing.
  28. * @author Rod Johnson
  29. */
  30. public class StaticApplicationContext extends AbstractApplicationContext {
  31. private DefaultListableBeanFactory beanFactory;
  32. /**
  33. * Create new StaticApplicationContext.
  34. */
  35. public StaticApplicationContext() throws BeansException {
  36. this(null);
  37. }
  38. /**
  39. * Create new StaticApplicationContext with the given parent.
  40. * @param parent the parent application context
  41. */
  42. public StaticApplicationContext(ApplicationContext parent) throws BeansException {
  43. super(parent);
  44. // create bean factory with parent
  45. this.beanFactory = new DefaultListableBeanFactory(getInternalParentBeanFactory());
  46. // Register the message source bean
  47. registerSingleton(MESSAGE_SOURCE_BEAN_NAME, StaticMessageSource.class, null);
  48. }
  49. /**
  50. * Return the underlying bean factory of this context.
  51. */
  52. public DefaultListableBeanFactory getDefaultListableBeanFactory() {
  53. return beanFactory;
  54. }
  55. /**
  56. * Return underlying bean factory for super class.
  57. */
  58. public ConfigurableListableBeanFactory getBeanFactory() {
  59. return beanFactory;
  60. }
  61. /**
  62. * Do nothing: We rely on callers to update our public methods.
  63. */
  64. protected void refreshBeanFactory() {
  65. }
  66. /**
  67. * Register a singleton bean with the default bean factory.
  68. */
  69. public void registerSingleton(String name, Class clazz, MutablePropertyValues pvs) throws BeansException {
  70. this.beanFactory.registerBeanDefinition(name, new RootBeanDefinition(clazz, pvs));
  71. }
  72. /**
  73. * Register a prototype bean with the default bean factory.
  74. */
  75. public void registerPrototype(String name, Class clazz, MutablePropertyValues pvs) throws BeansException {
  76. this.beanFactory.registerBeanDefinition(name, new RootBeanDefinition(clazz, pvs, false));
  77. }
  78. /**
  79. * Associate the given message with the given code.
  80. * @param code lookup code
  81. * @param locale locale message should be found within
  82. * @param defaultMessage message associated with this lookup code
  83. */
  84. public void addMessage(String code, Locale locale, String defaultMessage) {
  85. StaticMessageSource messageSource = (StaticMessageSource) getBean(MESSAGE_SOURCE_BEAN_NAME);
  86. messageSource.addMessage(code, locale, defaultMessage);
  87. }
  88. }