下面列出了org.springframework.context.annotation.Description#org.springframework.context.support.ResourceBundleMessageSource 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
* context-param and creates a corresponding child message source,
* with the provided Spring-defined MessageSource as parent.
* @param servletContext the ServletContext we're running in
* (to check JSTL-related context-params in {@code web.xml})
* @param messageSource the MessageSource to expose, typically
* the ApplicationContext of the current DispatcherServlet
* @return the MessageSource to expose to JSTL; first checking the
* JSTL-defined bundle, then the Spring-defined MessageSource
* @see org.springframework.context.ApplicationContext
*/
public static MessageSource getJstlAwareMessageSource(
@Nullable ServletContext servletContext, MessageSource messageSource) {
if (servletContext != null) {
String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
if (jstlInitParam != null) {
// Create a ResourceBundleMessageSource for the specified resource bundle
// basename in the JSTL context-param in web.xml, wiring it with the given
// Spring-defined MessageSource as parent.
ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
jstlBundleWrapper.setBasename(jstlInitParam);
jstlBundleWrapper.setParentMessageSource(messageSource);
return jstlBundleWrapper;
}
}
return messageSource;
}
/**
* Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
* context-param and creates a corresponding child message source,
* with the provided Spring-defined MessageSource as parent.
* @param servletContext the ServletContext we're running in
* (to check JSTL-related context-params in {@code web.xml})
* @param messageSource the MessageSource to expose, typically
* the ApplicationContext of the current DispatcherServlet
* @return the MessageSource to expose to JSTL; first checking the
* JSTL-defined bundle, then the Spring-defined MessageSource
* @see org.springframework.context.ApplicationContext
*/
public static MessageSource getJstlAwareMessageSource(
@Nullable ServletContext servletContext, MessageSource messageSource) {
if (servletContext != null) {
String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
if (jstlInitParam != null) {
// Create a ResourceBundleMessageSource for the specified resource bundle
// basename in the JSTL context-param in web.xml, wiring it with the given
// Spring-defined MessageSource as parent.
ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
jstlBundleWrapper.setBasename(jstlInitParam);
jstlBundleWrapper.setParentMessageSource(messageSource);
return jstlBundleWrapper;
}
}
return messageSource;
}
@Before
public void setUp() {
N2oEnvironment environment = new N2oEnvironment();
environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("n2o_messages", "messages");
messageSource.setDefaultEncoding("UTF-8");
environment.setMessageSource(new MessageSourceAccessor(messageSource));
OverrideProperties properties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
properties.put("n2o.engine.mapper", "spel");
environment.setSystemProperties(new SimplePropertyResolver(properties));
builder = new N2oApplicationBuilder(environment);
configure(builder);
CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
@Before
public void setUp() {
N2oEnvironment environment = new N2oEnvironment();
environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("n2o_messages", "messages");
messageSource.setDefaultEncoding("UTF-8");
environment.setMessageSource(new MessageSourceAccessor(messageSource));
OverrideProperties properties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
properties.put("n2o.engine.mapper", "spel");
environment.setSystemProperties(new SimplePropertyResolver(properties));
builder = new N2oApplicationBuilder(environment);
configure(builder);
CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
@Before
public void setUp() {
N2oEnvironment environment = new N2oEnvironment();
environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("n2o_messages", "messages");
messageSource.setDefaultEncoding("UTF-8");
environment.setMessageSource(new MessageSourceAccessor(messageSource));
OverrideProperties properties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
properties.put("n2o.engine.mapper", "spel");
SimplePropertyResolver propertyResolver = new SimplePropertyResolver(properties);
setUpStaticProperties(propertyResolver);
environment.setSystemProperties(propertyResolver);
builder = new N2oApplicationBuilder(environment);
configure(builder);
CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
public N2oEnvironment() {
this.metadataRegister = new N2oMetadataRegister();
this.routeRegister = new N2oRouteRegister();
this.sourceTypeRegister = new N2oSourceTypeRegister();
this.messageSource = new MessageSourceAccessor(new ResourceBundleMessageSource());
this.systemProperties = new N2oWebAppEnvironment();
this.domainProcessor = new DomainProcessor(new ObjectMapper());
this.contextProcessor = new ContextProcessor(new TestContextEngine());
this.namespaceReaderFactory = new ReaderFactoryByMap();
this.namespacePersisterFactory = new PersisterFactoryByMap();
this.dynamicMetadataProviderFactory = new N2oDynamicMetadataProviderFactory();
this.metadataScannerFactory = new N2oMetadataScannerFactory();
this.sourceLoaderFactory = new N2oSourceLoaderFactory();
this.sourceValidatorFactory = new N2oSourceValidatorFactory();
this.sourceCompilerFactory = new N2oSourceCompilerFactory();
this.compileTransformerFactory = new N2oCompileTransformerFactory();
this.sourceTransformerFactory = new N2oSourceTransformerFactory();
this.extensionAttributeMapperFactory = new N2oExtensionAttributeMapperFactory();
this.sourceMergerFactory = new N2oSourceMergerFactory();
this.metadataBinderFactory = new N2oMetadataBinderFactory();
this.pipelineOperationFactory = new N2oPipelineOperationFactory();
this.buttonGeneratorFactory = new N2oButtonGeneratorFactory();
}
@PostConstruct
public void init() {
logger.info("init MessageResourceExtension...");
if (!StringUtils.isEmpty(baseFolder)) {
try {
this.setBasenames(getAllBaseNames(baseFolder));
} catch (IOException e) {
logger.error(e.getMessage());
}
}
//设置父MessageSource
ResourceBundleMessageSource parent = new ResourceBundleMessageSource();
//是否是多个目录
if (basename.indexOf(",") > 0) {
parent.setBasenames(basename.split(","));
} else {
parent.setBasename(basename);
}
//设置文件编码
parent.setDefaultEncoding("UTF-8");
this.setParentMessageSource(parent);
}
/**
* Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
* context-param and creates a corresponding child message source,
* with the provided Spring-defined MessageSource as parent.
* @param servletContext the ServletContext we're running in
* (to check JSTL-related context-params in {@code web.xml})
* @param messageSource the MessageSource to expose, typically
* the ApplicationContext of the current DispatcherServlet
* @return the MessageSource to expose to JSTL; first checking the
* JSTL-defined bundle, then the Spring-defined MessageSource
* @see org.springframework.context.ApplicationContext
*/
public static MessageSource getJstlAwareMessageSource(
ServletContext servletContext, MessageSource messageSource) {
if (servletContext != null) {
String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
if (jstlInitParam != null) {
// Create a ResourceBundleMessageSource for the specified resource bundle
// basename in the JSTL context-param in web.xml, wiring it with the given
// Spring-defined MessageSource as parent.
ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
jstlBundleWrapper.setBasename(jstlInitParam);
jstlBundleWrapper.setParentMessageSource(messageSource);
return jstlBundleWrapper;
}
}
return messageSource;
}
@Override
public MessageSource getMessageService(String messageFilename) {
if (messageFilename != null) {
MessageSource ms = messageServices.get(messageFilename);
if (ms == null) {
ResourceBundleMessageSource rbms = (ResourceBundleMessageSource) beanFactory
.getBean(LOADED_MESSAGE_SOURCE_BEAN);
rbms.setBasename(messageFilename);
messageServices.put(messageFilename, rbms);
ms = rbms;
}
return ms;
} else {
return null;
}
}
@Override
public void onContextRefreshed() {
Set<String> messages = new HashSet<>();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
for (String beanName : BeanFactory.getBeanNames()) {
Package beanPackage = BeanFactory.getBeanClass(beanName).getPackage();
if (beanPackage == null) {
logger.warn(null, "无法获得Bean[{}]包。", beanName);
continue;
}
String packageName = beanPackage.getName();
if (resolver.getResource(packageName.replace('.', File.separatorChar) + "/message.properties").exists())
messages.add(packageName);
}
String[] names = new String[messages.size()];
int i = 0;
for (String name : messages)
names[i++] = name + ".message";
messageSource = new ResourceBundleMessageSource();
messageSource.setDefaultEncoding(context.getCharset(null));
messageSource.setBasenames(names);
}
/**
* Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
* context-param and creates a corresponding child message source,
* with the provided Spring-defined MessageSource as parent.
* @param servletContext the ServletContext we're running in
* (to check JSTL-related context-params in {@code web.xml})
* @param messageSource the MessageSource to expose, typically
* the ApplicationContext of the current DispatcherServlet
* @return the MessageSource to expose to JSTL; first checking the
* JSTL-defined bundle, then the Spring-defined MessageSource
* @see org.springframework.context.ApplicationContext
*/
public static MessageSource getJstlAwareMessageSource(
ServletContext servletContext, MessageSource messageSource) {
if (servletContext != null) {
String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
if (jstlInitParam != null) {
// Create a ResourceBundleMessageSource for the specified resource bundle
// basename in the JSTL context-param in web.xml, wiring it with the given
// Spring-defined MessageSource as parent.
ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
jstlBundleWrapper.setBasename(jstlInitParam);
jstlBundleWrapper.setParentMessageSource(messageSource);
return jstlBundleWrapper;
}
}
return messageSource;
}
protected ExceptionHandlerExceptionResolver createExceptionResolver() {
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("rest/messages");
messageSource.setUseCodeAsDefaultMessage(true);
ExceptionHandlerExceptionResolver exceptionResolver = new ExceptionHandlerExceptionResolver() {
@Override
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
Method method = new ExceptionHandlerMethodResolver(RestExceptionHandler.class).resolveMethod(exception);
RestExceptionHandler validationHandler = new RestExceptionHandler();
validationHandler.setMessageSource(messageSource);
return new ServletInvocableHandlerMethod(validationHandler, method);
}
};
exceptionResolver.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
exceptionResolver.afterPropertiesSet();
return exceptionResolver;
}
@Override
protected MessageSource createMessageSource(String basename) {
ResourceBundleMessageSource messageSource = (ResourceBundleMessageSource) super.createMessageSource(basename);
// Create parent theme recursively.
for (Theme theme : settingsService.getAvailableThemes()) {
if ((basenamePrefix + theme.getId()).equals(basename) && theme.getParent() != null) {
String parent = basenamePrefix + theme.getParent();
messageSource.setParentMessageSource(createMessageSource(parent));
break;
}
}
return messageSource;
}
/**
* Create a MessageSource for the given basename,
* to be used as MessageSource for the corresponding theme.
* <p>Default implementation creates a ResourceBundleMessageSource.
* for the given basename. A subclass could create a specifically
* configured ReloadableResourceBundleMessageSource, for example.
* @param basename the basename to create a MessageSource for
* @return the MessageSource
* @see org.springframework.context.support.ResourceBundleMessageSource
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource
*/
protected MessageSource createMessageSource(String basename) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(basename);
if (this.defaultEncoding != null) {
messageSource.setDefaultEncoding(this.defaultEncoding);
}
if (this.fallbackToSystemLocale != null) {
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
}
if (this.beanClassLoader != null) {
messageSource.setBeanClassLoader(this.beanClassLoader);
}
return messageSource;
}
/**
* 国际化
*
* @return
*/
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
/**
* 此处有很多坑,不一一叙述,如无必要,不要更改
*
* @return
*/
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
/**
* 国际化
*
* @return
*/
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
public static void main(String[] args) throws InterruptedException {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages/messages");
System.err.println(messageSource.getMessage("welcome", new Object[0], Locale.UK));
ClassPathResource resource = new ClassPathResource("META-INF/resources/webjars/jquery/2.2.4/jquery.js");
System.err.println(resource.exists());
Thread.currentThread().join(); // To be able to measure memory consumption
}
/**
* 国际化
*
* @return
*/
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
/**
* 此处有很多坑,不一一叙述,如无必要,不要更改
*
* @return
*/
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
/**
* 国际化
*
* @return
*/
@Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages", "i18n/ValidationMessages");
resourceBundleMessageSource.setCacheSeconds(3600);
return resourceBundleMessageSource;
}
/**
* Create a MessageSource for the given basename,
* to be used as MessageSource for the corresponding theme.
* <p>Default implementation creates a ResourceBundleMessageSource.
* for the given basename. A subclass could create a specifically
* configured ReloadableResourceBundleMessageSource, for example.
* @param basename the basename to create a MessageSource for
* @return the MessageSource
* @see org.springframework.context.support.ResourceBundleMessageSource
* @see org.springframework.context.support.ReloadableResourceBundleMessageSource
*/
protected MessageSource createMessageSource(String basename) {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(basename);
if (this.defaultEncoding != null) {
messageSource.setDefaultEncoding(this.defaultEncoding);
}
if (this.fallbackToSystemLocale != null) {
messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
}
if (this.beanClassLoader != null) {
messageSource.setBeanClassLoader(this.beanClassLoader);
}
return messageSource;
}
@Bean("n2oMessageSource")
@ConditionalOnMissingBean(name = "n2oMessageSource")
public MessageSource n2oMessageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setDefaultEncoding(encoding.name());
messageSource.setCacheSeconds(cacheSeconds);
messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(basename)));
messageSource.addBasenames("n2o_messages", "n2o_content");
return messageSource;
}
public void setUp() throws Exception {
N2oEnvironment environment = new N2oEnvironment();
environment.setNamespacePersisterFactory(new PersisterFactoryByMap());
environment.setNamespaceReaderFactory(new ReaderFactoryByMap());
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("n2o_messages", "messages");
messageSource.setDefaultEncoding("UTF-8");
environment.setMessageSource(new MessageSourceAccessor(messageSource));
OverrideProperties properties = PropertiesReader.getPropertiesFromClasspath("META-INF/n2o.properties");
environment.setSystemProperties(new SimplePropertyResolver(properties));
builder = new N2oApplicationBuilder(environment);
configure(builder);
CompileInfo.setSourceTypes(builder.getEnvironment().getSourceTypeRegister());
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename(env.getRequiredProperty("message.source.basename"));
source.setUseCodeAsDefaultMessage(true);
source.setDefaultEncoding("UTF-8");
// # -1 : never reload, 0 always reload
source.setCacheSeconds(0);
return source;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename(env.getRequiredProperty("message.source.basename"));
source.setUseCodeAsDefaultMessage(true);
source.setDefaultEncoding("UTF-8");
// # -1 : never reload, 0 always reload
source.setCacheSeconds(0);
return source;
}
/**
* Set the source of i18n messages under "resources/i18n/messages_[locale].properties"
*
* @return instance of {@link ResourceBundleMessageSource}
*/
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasenames("i18n/messages");
source.setUseCodeAsDefaultMessage(true);
return source;
}
/**
* Set the source of i18n messages under "resources/i18n/messages_[locale].properties"
*
* @return instance of {@link ResourceBundleMessageSource}
*/
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasenames("i18n/messages");
source.setUseCodeAsDefaultMessage(true);
return source;
}