001/* 002 * Copyright 2002-2013 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 * https://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 017package org.springframework.context.support; 018 019import java.util.Locale; 020 021import org.springframework.beans.BeansException; 022import org.springframework.beans.MutablePropertyValues; 023import org.springframework.beans.factory.support.GenericBeanDefinition; 024import 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 */ 039public class StaticApplicationContext extends GenericApplicationContext { 040 041 private final StaticMessageSource staticMessageSource; 042 043 044 /** 045 * Create a new StaticApplicationContext. 046 * @see #registerSingleton 047 * @see #registerPrototype 048 * @see #registerBeanDefinition 049 * @see #refresh 050 */ 051 public StaticApplicationContext() throws BeansException { 052 this(null); 053 } 054 055 /** 056 * Create a new StaticApplicationContext with the given parent. 057 * @see #registerSingleton 058 * @see #registerPrototype 059 * @see #registerBeanDefinition 060 * @see #refresh 061 */ 062 public StaticApplicationContext(ApplicationContext parent) throws BeansException { 063 super(parent); 064 065 // Initialize and register a StaticMessageSource. 066 this.staticMessageSource = new StaticMessageSource(); 067 getBeanFactory().registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.staticMessageSource); 068 } 069 070 071 /** 072 * Overridden to turn it into a no-op, to be more lenient towards test cases. 073 */ 074 @Override 075 protected void assertBeanFactoryActive() { 076 } 077 078 /** 079 * Return the internal StaticMessageSource used by this context. 080 * Can be used to register messages on it. 081 * @see #addMessage 082 */ 083 public final StaticMessageSource getStaticMessageSource() { 084 return this.staticMessageSource; 085 } 086 087 /** 088 * Register a singleton bean with the underlying bean factory. 089 * <p>For more advanced needs, register with the underlying BeanFactory directly. 090 * @see #getDefaultListableBeanFactory 091 */ 092 public void registerSingleton(String name, Class<?> clazz) throws BeansException { 093 GenericBeanDefinition bd = new GenericBeanDefinition(); 094 bd.setBeanClass(clazz); 095 getDefaultListableBeanFactory().registerBeanDefinition(name, bd); 096 } 097 098 /** 099 * Register a singleton 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 registerSingleton(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { 104 GenericBeanDefinition bd = new GenericBeanDefinition(); 105 bd.setBeanClass(clazz); 106 bd.setPropertyValues(pvs); 107 getDefaultListableBeanFactory().registerBeanDefinition(name, bd); 108 } 109 110 /** 111 * Register a prototype bean with the underlying bean factory. 112 * <p>For more advanced needs, register with the underlying BeanFactory directly. 113 * @see #getDefaultListableBeanFactory 114 */ 115 public void registerPrototype(String name, Class<?> clazz) throws BeansException { 116 GenericBeanDefinition bd = new GenericBeanDefinition(); 117 bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); 118 bd.setBeanClass(clazz); 119 getDefaultListableBeanFactory().registerBeanDefinition(name, bd); 120 } 121 122 /** 123 * Register a prototype bean with the underlying bean factory. 124 * <p>For more advanced needs, register with the underlying BeanFactory directly. 125 * @see #getDefaultListableBeanFactory 126 */ 127 public void registerPrototype(String name, Class<?> clazz, MutablePropertyValues pvs) throws BeansException { 128 GenericBeanDefinition bd = new GenericBeanDefinition(); 129 bd.setScope(GenericBeanDefinition.SCOPE_PROTOTYPE); 130 bd.setBeanClass(clazz); 131 bd.setPropertyValues(pvs); 132 getDefaultListableBeanFactory().registerBeanDefinition(name, bd); 133 } 134 135 /** 136 * Associate the given message with the given code. 137 * @param code lookup code 138 * @param locale locale message should be found within 139 * @param defaultMessage message associated with this lookup code 140 * @see #getStaticMessageSource 141 */ 142 public void addMessage(String code, Locale locale, String defaultMessage) { 143 getStaticMessageSource().addMessage(code, locale, defaultMessage); 144 } 145 146}