use of org.springframework.context.MessageSource in project spring-framework by spring-projects.
the class JstlUtils method exposeLocalizationContext.
/**
* Exposes JSTL-specific request attributes specifying locale
* and resource bundle for JSTL's formatting and message tags,
* using Spring's locale and MessageSource.
* @param requestContext the context for the current HTTP request,
* including the ApplicationContext to expose as MessageSource
*/
public static void exposeLocalizationContext(RequestContext requestContext) {
Config.set(requestContext.getRequest(), Config.FMT_LOCALE, requestContext.getLocale());
TimeZone timeZone = requestContext.getTimeZone();
if (timeZone != null) {
Config.set(requestContext.getRequest(), Config.FMT_TIME_ZONE, timeZone);
}
MessageSource messageSource = getJstlAwareMessageSource(requestContext.getServletContext(), requestContext.getMessageSource());
LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, requestContext.getRequest());
Config.set(requestContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
use of org.springframework.context.MessageSource in project spring-framework by spring-projects.
the class ClassPathXmlApplicationContextTests method testMessageSourceAware.
@Test
public void testMessageSourceAware() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONTEXT_WILDCARD);
MessageSource messageSource = (MessageSource) ctx.getBean("messageSource");
Service service1 = (Service) ctx.getBean("service");
assertEquals(ctx, service1.getMessageSource());
Service service2 = (Service) ctx.getBean("service2");
assertEquals(ctx, service2.getMessageSource());
AutowiredService autowiredService1 = (AutowiredService) ctx.getBean("autowiredService");
assertEquals(messageSource, autowiredService1.getMessageSource());
AutowiredService autowiredService2 = (AutowiredService) ctx.getBean("autowiredService2");
assertEquals(messageSource, autowiredService2.getMessageSource());
ctx.close();
}
use of org.springframework.context.MessageSource in project spring-framework by spring-projects.
the class AutoProxyCreatorTests method testBeanNameAutoProxyCreator.
@Test
public void testBeanNameAutoProxyCreator() {
StaticApplicationContext sac = new StaticApplicationContext();
sac.registerSingleton("testInterceptor", TestInterceptor.class);
RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class);
proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor");
proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,innerBean,singletonFactoryToBeProxied");
sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
RootBeanDefinition innerBean = new RootBeanDefinition(TestBean.class);
bd.getPropertyValues().add("spouse", new BeanDefinitionHolder(innerBean, "innerBean"));
sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);
sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
sac.registerSingleton("autowiredIndexedTestBean", IndexedTestBean.class);
sac.refresh();
MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
assertFalse(Proxy.isProxyClass(messageSource.getClass()));
assertTrue(Proxy.isProxyClass(singletonToBeProxied.getClass()));
assertTrue(Proxy.isProxyClass(singletonToBeProxied.getSpouse().getClass()));
// test whether autowiring succeeded with auto proxy creation
assertEquals(sac.getBean("autowiredIndexedTestBean"), singletonToBeProxied.getNestedIndexedBean());
TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor");
// already 2: getSpouse + getNestedIndexedBean calls above
assertEquals(2, ti.nrOfInvocations);
singletonToBeProxied.getName();
singletonToBeProxied.getSpouse().getName();
assertEquals(5, ti.nrOfInvocations);
ITestBean tb = (ITestBean) sac.getBean("singletonFactoryToBeProxied");
assertTrue(AopUtils.isJdkDynamicProxy(tb));
assertEquals(5, ti.nrOfInvocations);
tb.getAge();
assertEquals(6, ti.nrOfInvocations);
ITestBean tb2 = (ITestBean) sac.getBean("singletonFactoryToBeProxied");
assertSame(tb, tb2);
assertEquals(6, ti.nrOfInvocations);
tb2.getAge();
assertEquals(7, ti.nrOfInvocations);
}
Aggregations