Search in sources :

Example 11 with MessageSource

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);
}
Also used : TimeZone(java.util.TimeZone) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) MessageSource(org.springframework.context.MessageSource)

Example 12 with MessageSource

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();
}
Also used : MessageSource(org.springframework.context.MessageSource) Test(org.junit.Test)

Example 13 with MessageSource

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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) BeanDefinitionHolder(org.springframework.beans.factory.config.BeanDefinitionHolder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.Test)

Aggregations

MessageSource (org.springframework.context.MessageSource)13 Test (org.junit.Test)7 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)4 StaticMessageSource (org.springframework.context.support.StaticMessageSource)4 ITestBean (org.springframework.tests.sample.beans.ITestBean)4 PersonnelServiceFacade (org.mifos.application.admin.servicefacade.PersonnelServiceFacade)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 HierarchicalMessageSource (org.springframework.context.HierarchicalMessageSource)2 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)2 FooServiceImpl (example.scannable.FooServiceImpl)1 Locale (java.util.Locale)1 TimeZone (java.util.TimeZone)1 LocalizationContext (javax.servlet.jsp.jstl.fmt.LocalizationContext)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanDefinitionHolder (org.springframework.beans.factory.config.BeanDefinitionHolder)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 StaticListableBeanFactory (org.springframework.beans.factory.support.StaticListableBeanFactory)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 Theme (org.springframework.ui.context.Theme)1