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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations