Search in sources :

Example 6 with MessageSource

use of org.springframework.context.MessageSource in project spring-framework by spring-projects.

the class AutoProxyCreatorTests method testCustomAutoProxyCreator.

@Test
public void testCustomAutoProxyCreator() {
    StaticApplicationContext sac = new StaticApplicationContext();
    sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.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));
    assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
    assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
    assertTrue(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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) Test(org.junit.Test)

Example 7 with MessageSource

use of org.springframework.context.MessageSource in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerTests method testBeanAutowiredWithAnnotationConfigEnabled.

@Test
public void testBeanAutowiredWithAnnotationConfigEnabled() {
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("myBf", new RootBeanDefinition(StaticListableBeanFactory.class));
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setBeanNameGenerator(new TestBeanNameGenerator());
    int beanCount = scanner.scan(BASE_PACKAGE);
    assertEquals(12, beanCount);
    context.refresh();
    FooServiceImpl fooService = context.getBean("fooService", FooServiceImpl.class);
    StaticListableBeanFactory myBf = (StaticListableBeanFactory) context.getBean("myBf");
    MessageSource ms = (MessageSource) context.getBean("messageSource");
    assertTrue(fooService.isInitCalled());
    assertEquals("bar", fooService.foo(123));
    assertEquals("bar", fooService.lookupFoo(123));
    assertSame(context.getDefaultListableBeanFactory(), fooService.beanFactory);
    assertEquals(2, fooService.listableBeanFactory.size());
    assertSame(context.getDefaultListableBeanFactory(), fooService.listableBeanFactory.get(0));
    assertSame(myBf, fooService.listableBeanFactory.get(1));
    assertSame(context, fooService.resourceLoader);
    assertSame(context, fooService.resourcePatternResolver);
    assertSame(context, fooService.eventPublisher);
    assertSame(ms, fooService.messageSource);
    assertSame(context, fooService.context);
    assertEquals(1, fooService.configurableContext.length);
    assertSame(context, fooService.configurableContext[0]);
    assertSame(context, fooService.genericContext);
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) StaticListableBeanFactory(org.springframework.beans.factory.support.StaticListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MessageSource(org.springframework.context.MessageSource) Test(org.junit.Test)

Example 8 with MessageSource

use of org.springframework.context.MessageSource in project spring-security by spring-projects.

the class JdbcDaoImplTests method setMessageSourceWhenNotNullThenCanGet.

@Test
public void setMessageSourceWhenNotNullThenCanGet() throws Exception {
    MessageSource source = mock(MessageSource.class);
    JdbcDaoImpl dao = new JdbcDaoImpl();
    dao.setMessageSource(source);
    String code = "code";
    dao.getMessages().getMessage(code);
    verify(source).getMessage(eq(code), any(Object[].class), any(Locale.class));
}
Also used : Locale(java.util.Locale) MessageSource(org.springframework.context.MessageSource) Test(org.junit.Test)

Example 9 with MessageSource

use of org.springframework.context.MessageSource in project head by mifos.

the class MessageLookup method getLocalizedMessage.

public static String getLocalizedMessage(String key) {
    // nothing found so return the key
    String message = key;
    PersonnelServiceFacade personnelServiceFacade = ApplicationContextProvider.getBean(PersonnelServiceFacade.class);
    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;
}
Also used : MessageSource(org.springframework.context.MessageSource) PersonnelServiceFacade(org.mifos.application.admin.servicefacade.PersonnelServiceFacade)

Example 10 with MessageSource

use of org.springframework.context.MessageSource in project head by mifos.

the class BaseActionForm method getLocalizedMessage.

protected String getLocalizedMessage(String key) {
    // nothing found so return the key
    String message = key;
    PersonnelServiceFacade personnelServiceFacade = ApplicationContextProvider.getBean(PersonnelServiceFacade.class);
    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;
}
Also used : MessageSource(org.springframework.context.MessageSource) PersonnelServiceFacade(org.mifos.application.admin.servicefacade.PersonnelServiceFacade)

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