Search in sources :

Example 1 with MessageSource

use of cn.taketoday.context.MessageSource in project today-framework by TAKETODAY.

the class ClassPathBeanDefinitionScannerTests method testBeanAutowiredWithAnnotationConfigEnabled.

@Test
public void testBeanAutowiredWithAnnotationConfigEnabled() {
    GenericApplicationContext context = new GenericApplicationContext();
    context.registerBeanDefinition("myBf", new RootBeanDefinition(StandardBeanFactory.class));
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
    scanner.setBeanNamePopulator(new TestBeanNamePopulator());
    int beanCount = scanner.scan(BASE_PACKAGE);
    assertThat(beanCount).isGreaterThanOrEqualTo(9);
    context.refresh();
    FooServiceImpl fooService = context.getBean("fooService", FooServiceImpl.class);
    StandardBeanFactory myBf = (StandardBeanFactory) context.getBean("myBf");
    MessageSource ms = (MessageSource) context.getBean("messageSource");
    assertThat(fooService.isInitCalled()).isTrue();
    assertThat(fooService.foo(113)).isEqualTo("bar");
    assertThat(fooService.lookupFoo(113)).isEqualTo("bar");
    assertThat(fooService.beanFactory).isSameAs(context.getBeanFactory());
    assertThat(fooService.BeanFactory.size()).isEqualTo(2);
    assertThat(fooService.BeanFactory.get(0)).isSameAs(context.getBeanFactory());
    assertThat(fooService.BeanFactory.get(1)).isSameAs(myBf);
    assertThat(fooService.resourceLoader).isSameAs(context);
    assertThat(fooService.resourcePatternResolver).isSameAs(context);
    assertThat(fooService.eventPublisher).isSameAs(context);
    assertThat(fooService.messageSource).isSameAs(ms);
    assertThat(fooService.context).isSameAs(context);
    assertThat(fooService.configurableContext.length).isEqualTo(1);
    assertThat(fooService.configurableContext[0]).isSameAs(context);
    assertThat(fooService.genericContext).isSameAs(context);
}
Also used : FooServiceImpl(example.scannable.FooServiceImpl) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) MessageSource(cn.taketoday.context.MessageSource) StandardBeanFactory(cn.taketoday.beans.factory.support.StandardBeanFactory) Test(org.junit.jupiter.api.Test)

Example 2 with MessageSource

use of cn.taketoday.context.MessageSource in project today-framework by TAKETODAY.

the class JstlUtils method exposeLocalizationContext.

/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Framework's locale and MessageSource.
 *
 * @param request the context for the current HTTP request,
 * including the ApplicationContext to expose as MessageSource
 */
public static void exposeLocalizationContext(RequestContext request, HttpServletRequest servletRequest) {
    Locale locale = null;
    TimeZone timeZone = null;
    // Determine locale to use for this RequestContext.
    LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
    if (localeResolver instanceof LocaleContextResolver) {
        LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
        locale = localeContext.getLocale();
        if (localeContext instanceof TimeZoneAwareLocaleContext) {
            timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
        }
    } else if (localeResolver != null) {
        // Try LocaleResolver (we're within a DispatcherServlet request).
        locale = localeResolver.resolveLocale(request);
    }
    // fallback
    if (locale == null) {
        locale = getLocale(servletRequest);
    }
    if (timeZone == null) {
        timeZone = getTimeZone(servletRequest);
    }
    if (locale != null) {
        Config.set(servletRequest, Config.FMT_LOCALE, locale);
    }
    if (timeZone != null) {
        Config.set(servletRequest, Config.FMT_TIME_ZONE, timeZone);
    }
    MessageSource messageSource = getJstlAwareMessageSource(servletRequest.getServletContext(), request.getApplicationContext());
    LocalizationContext jstlContext = new FrameworkLocalizationContext(messageSource, servletRequest, request);
    Config.set(servletRequest, Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
Also used : Locale(java.util.Locale) LocaleResolver(cn.taketoday.web.LocaleResolver) TimeZone(java.util.TimeZone) TimeZoneAwareLocaleContext(cn.taketoday.core.i18n.TimeZoneAwareLocaleContext) LocaleContext(cn.taketoday.core.i18n.LocaleContext) LocalizationContext(jakarta.servlet.jsp.jstl.fmt.LocalizationContext) TimeZoneAwareLocaleContext(cn.taketoday.core.i18n.TimeZoneAwareLocaleContext) MessageSource(cn.taketoday.context.MessageSource) ResourceBundleMessageSource(cn.taketoday.context.support.ResourceBundleMessageSource) LocaleContextResolver(cn.taketoday.web.LocaleContextResolver)

Example 3 with MessageSource

use of cn.taketoday.context.MessageSource in project today-framework by TAKETODAY.

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");
    assertThat(AopUtils.isCglibProxy(messageSource)).isFalse();
    assertThat(AopUtils.isCglibProxy(noInterfaces)).isTrue();
    assertThat(AopUtils.isCglibProxy(containerCallbackInterfacesOnly)).isTrue();
    assertThat(AopUtils.isCglibProxy(singletonNoInterceptor)).isTrue();
    assertThat(AopUtils.isCglibProxy(singletonToBeProxied)).isTrue();
    assertThat(AopUtils.isCglibProxy(prototypeToBeProxied)).isTrue();
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
    singletonNoInterceptor.getName();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
    singletonToBeProxied.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
    prototypeToBeProxied.getSpouse();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) StaticMessageSource(cn.taketoday.context.support.StaticMessageSource) MessageSource(cn.taketoday.context.MessageSource) Test(org.junit.jupiter.api.Test)

Example 4 with MessageSource

use of cn.taketoday.context.MessageSource in project today-framework by TAKETODAY.

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");
    assertThat(AopUtils.isCglibProxy(messageSource)).isFalse();
    assertThat(AopUtils.isCglibProxy(noInterfaces)).isTrue();
    assertThat(AopUtils.isCglibProxy(containerCallbackInterfacesOnly)).isTrue();
    assertThat(AopUtils.isCglibProxy(singletonNoInterceptor)).isFalse();
    assertThat(AopUtils.isCglibProxy(singletonToBeProxied)).isFalse();
    assertThat(AopUtils.isCglibProxy(prototypeToBeProxied)).isFalse();
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
    singletonNoInterceptor.getName();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
    singletonToBeProxied.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(1);
    prototypeToBeProxied.getSpouse();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) StaticMessageSource(cn.taketoday.context.support.StaticMessageSource) MessageSource(cn.taketoday.context.MessageSource) Test(org.junit.jupiter.api.Test)

Example 5 with MessageSource

use of cn.taketoday.context.MessageSource in project today-framework by TAKETODAY.

the class AutoProxyCreatorTests method testAutoProxyCreatorWithFallbackToDynamicProxy.

@Test
public void testAutoProxyCreatorWithFallbackToDynamicProxy() {
    StaticApplicationContext sac = new StaticApplicationContext();
    PropertyValues pvs = new PropertyValues();
    pvs.add("proxyFactoryBean", "false");
    sac.registerSingleton("testAutoProxyCreator", IntroductionTestAutoProxyCreator.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");
    assertThat(AopUtils.isCglibProxy(messageSource)).isFalse();
    assertThat(AopUtils.isCglibProxy(noInterfaces)).isTrue();
    assertThat(AopUtils.isCglibProxy(containerCallbackInterfacesOnly)).isTrue();
    assertThat(AopUtils.isCglibProxy(singletonNoInterceptor)).isFalse();
    assertThat(AopUtils.isCglibProxy(singletonToBeProxied)).isFalse();
    assertThat(AopUtils.isCglibProxy(prototypeToBeProxied)).isFalse();
    TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
    singletonNoInterceptor.getName();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(0);
    singletonToBeProxied.getAge();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(2);
    prototypeToBeProxied.getSpouse();
    assertThat(tapc.testInterceptor.nrOfInvocations).isEqualTo(4);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) PropertyValues(cn.taketoday.beans.PropertyValues) StaticApplicationContext(cn.taketoday.context.support.StaticApplicationContext) StaticMessageSource(cn.taketoday.context.support.StaticMessageSource) MessageSource(cn.taketoday.context.MessageSource) Test(org.junit.jupiter.api.Test)

Aggregations

MessageSource (cn.taketoday.context.MessageSource)15 Test (org.junit.jupiter.api.Test)13 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)8 StaticApplicationContext (cn.taketoday.context.support.StaticApplicationContext)8 StaticMessageSource (cn.taketoday.context.support.StaticMessageSource)8 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)4 PropertyValues (cn.taketoday.beans.PropertyValues)2 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)2 ResourceBundleMessageSource (cn.taketoday.context.support.ResourceBundleMessageSource)2 LocaleContext (cn.taketoday.core.i18n.LocaleContext)2 TimeZoneAwareLocaleContext (cn.taketoday.core.i18n.TimeZoneAwareLocaleContext)2 LocaleContextResolver (cn.taketoday.web.LocaleContextResolver)2 LocaleResolver (cn.taketoday.web.LocaleResolver)2 FooServiceImpl (example.scannable.FooServiceImpl)2 LocalizationContext (jakarta.servlet.jsp.jstl.fmt.LocalizationContext)2 MessageInterpolator (jakarta.validation.MessageInterpolator)2 Locale (java.util.Locale)2 TimeZone (java.util.TimeZone)2 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)2