use of org.springframework.context.MessageSource in project head by mifos.
the class BaseAction method getLocalizedMessage.
protected String getLocalizedMessage(String key) {
// nothing found so return the key
String message = key;
MessageSource messageSource = ApplicationContextProvider.getBean(MessageSource.class);
if (personnelServiceFacade != null) {
String value = messageSource.getMessage(key, null, personnelServiceFacade.getUserPreferredLocale());
if (StringUtils.isNotEmpty(message)) {
message = value;
}
}
return message;
}
use of org.springframework.context.MessageSource in project spring-framework by spring-projects.
the class AbstractApplicationContext method initMessageSource.
/**
* Initialize the MessageSource.
* Use parent's if none defined in this context.
*/
protected void initMessageSource() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
// Make MessageSource aware of parent MessageSource.
if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
if (hms.getParentMessageSource() == null) {
// Only set parent context as parent MessageSource if no parent MessageSource
// registered already.
hms.setParentMessageSource(getInternalParentMessageSource());
}
}
if (logger.isDebugEnabled()) {
logger.debug("Using MessageSource [" + this.messageSource + "]");
}
} else {
// Use empty MessageSource to be able to accept getMessage calls.
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME + "': using default [" + this.messageSource + "]");
}
}
}
use of org.springframework.context.MessageSource in project spring-framework by spring-projects.
the class ResourceBundleThemeSource method getTheme.
/**
* This implementation returns a SimpleTheme instance, holding a
* ResourceBundle-based MessageSource whose basename corresponds to
* the given theme name (prefixed by the configured "basenamePrefix").
* <p>SimpleTheme instances are cached per theme name. Use a reloadable
* MessageSource if themes should reflect changes to the underlying files.
* @see #setBasenamePrefix
* @see #createMessageSource
*/
@Override
public Theme getTheme(String themeName) {
if (themeName == null) {
return null;
}
Theme theme = this.themeCache.get(themeName);
if (theme == null) {
synchronized (this.themeCache) {
theme = this.themeCache.get(themeName);
if (theme == null) {
String basename = this.basenamePrefix + themeName;
MessageSource messageSource = createMessageSource(basename);
theme = new SimpleTheme(themeName, messageSource);
initParent(theme);
this.themeCache.put(themeName, theme);
if (logger.isDebugEnabled()) {
logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
}
}
}
}
return theme;
}
use of org.springframework.context.MessageSource in project spring-framework by spring-projects.
the class AutoProxyCreatorTests method testAutoProxyCreatorWithFallbackToTargetClass.
@Test
public void testAutoProxyCreatorWithFallbackToTargetClass() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
sac.registerSingleton("noInterfaces", NoInterfaces.class);
sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
sac.registerSingleton("singletonNoInterceptor", TestBean.class);
sac.registerSingleton("singletonToBeProxied", TestBean.class);
sac.registerPrototype("prototypeToBeProxied", TestBean.class);
sac.refresh();
MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly = (ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
assertFalse(AopUtils.isCglibProxy(messageSource));
assertTrue(AopUtils.isCglibProxy(noInterfaces));
assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
assertEquals(0, tapc.testInterceptor.nrOfInvocations);
singletonNoInterceptor.getName();
assertEquals(0, tapc.testInterceptor.nrOfInvocations);
singletonToBeProxied.getAge();
assertEquals(1, tapc.testInterceptor.nrOfInvocations);
prototypeToBeProxied.getSpouse();
assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
use of org.springframework.context.MessageSource in project spring-framework by spring-projects.
the class AutoProxyCreatorTests method testAutoProxyCreatorWithFallbackToDynamicProxy.
@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
StaticApplicationContext sac = new StaticApplicationContext();
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("proxyFactoryBean", "false");
sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);
sac.registerSingleton("noInterfaces", NoInterfaces.class);
sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class);
sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class);
sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.class);
sac.refresh();
MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly = (ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
assertFalse(AopUtils.isCglibProxy(messageSource));
assertTrue(AopUtils.isCglibProxy(noInterfaces));
assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));
TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
assertEquals(0, tapc.testInterceptor.nrOfInvocations);
singletonNoInterceptor.getName();
assertEquals(0, tapc.testInterceptor.nrOfInvocations);
singletonToBeProxied.getAge();
assertEquals(1, tapc.testInterceptor.nrOfInvocations);
prototypeToBeProxied.getSpouse();
assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
Aggregations