java类org.springframework.context.MessageSource的实例源码

ClassUtilsTest.java 文件源码 项目:gemini.blueprint 阅读 38 收藏 0 点赞 0 评论 0
public void testAppContextClassHierarchy() {
    Class<?>[] clazz =
            ClassUtils.getClassHierarchy(OsgiBundleXmlApplicationContext.class, ClassUtils.ClassSet.ALL_CLASSES);

       //Closeable.class,
    Class<?>[] expected =
            new Class<?>[] { OsgiBundleXmlApplicationContext.class,
                    AbstractDelegatedExecutionApplicationContext.class, AbstractOsgiBundleApplicationContext.class,
                    AbstractRefreshableApplicationContext.class, AbstractApplicationContext.class,
                    DefaultResourceLoader.class, ResourceLoader.class,
                    AutoCloseable.class,
                    DelegatedExecutionOsgiBundleApplicationContext.class,
                    ConfigurableOsgiBundleApplicationContext.class, ConfigurableApplicationContext.class,
                    ApplicationContext.class, Lifecycle.class, Closeable.class, EnvironmentCapable.class, ListableBeanFactory.class,
                    HierarchicalBeanFactory.class, ApplicationEventPublisher.class, ResourcePatternResolver.class,
                    MessageSource.class, BeanFactory.class, DisposableBean.class };

    assertTrue(compareArrays(expected, clazz));
}
AbstractMessageSource.java 文件源码 项目:lams 阅读 33 收藏 0 点赞 0 评论 0
/**
 * Try to retrieve the given message from the parent MessageSource, if any.
 * @param code the code to lookup up, such as 'calculator.noRateSet'
 * @param args array of arguments that will be filled in for params
 * within the message
 * @param locale the Locale in which to do the lookup
 * @return the resolved message, or {@code null} if not found
 * @see #getParentMessageSource()
 */
protected String getMessageFromParent(String code, Object[] args, Locale locale) {
    MessageSource parent = getParentMessageSource();
    if (parent != null) {
        if (parent instanceof AbstractMessageSource) {
            // Call internal method to avoid getting the default code back
            // in case of "useCodeAsDefaultMessage" being activated.
            return ((AbstractMessageSource) parent).getMessageInternal(code, args, locale);
        }
        else {
            // Check parent MessageSource, returning null if not found there.
            return parent.getMessage(code, args, null, locale);
        }
    }
    // Not found in parent either.
    return null;
}
ResourceBundleThemeSource.java 文件源码 项目:lams 阅读 45 收藏 0 点赞 0 评论 0
/**
 * This implementation returns a SimpleTheme instance, holding a
 * ResourceBundle-based MessageSource whose basename corresponds to
 * the given theme name (prefixed by the configured "basenamePrefix").
 * <p>SimpleTheme instances are cached per theme name. Use a reloadable
 * MessageSource if themes should reflect changes to the underlying files.
 * @see #setBasenamePrefix
 * @see #createMessageSource
 */
@Override
public Theme getTheme(String themeName) {
    if (themeName == null) {
        return null;
    }
    synchronized (this.themeCache) {
        Theme theme = this.themeCache.get(themeName);
        if (theme == null) {
            String basename = this.basenamePrefix + themeName;
            MessageSource messageSource = createMessageSource(basename);
            theme = new SimpleTheme(themeName, messageSource);
            initParent(theme);
            this.themeCache.put(themeName, theme);
            if (logger.isDebugEnabled()) {
                logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
            }
        }
        return theme;
    }
}
LoadedMessageSourceService.java 文件源码 项目:lams 阅读 28 收藏 0 点赞 0 评论 0
@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;
}
   }
MessageSourceUtil.java 文件源码 项目:asura 阅读 29 收藏 0 点赞 0 评论 0
/**
 * 查找错误消息
 *
 * @param source
 * @param code
 * @param params
 * @param defaultMsg
 * @return
 */
public static String getChinese(MessageSource source, String code, Object[] params, String defaultMsg) {
    if (Check.NuNObj(source)) {
        throw new IllegalArgumentException("message source is null");
    }
    if (Check.NuNStrStrict(code)) {
        throw new IllegalArgumentException("code is empty");
    }
    //如果没有object 默认设置为空数组
    if (Check.NuNObj(params)) {
        params = new Object[]{};
    }
    if (Check.NuNStrStrict(defaultMsg)) {
        return source.getMessage(code, params, Locale.SIMPLIFIED_CHINESE);
    } else {
        return source.getMessage(code, params, defaultMsg, Locale.SIMPLIFIED_CHINESE);
    }
}
MessageSourceUtil.java 文件源码 项目:asura 阅读 31 收藏 0 点赞 0 评论 0
/**
 * 查找错误消息
 *
 * @param source
 * @param code
 * @param params
 * @param defaultMsg
 * @return
 */
public static int getIntMessage(MessageSource source, String code, Object[] params, String defaultMsg) {
    if (Check.NuNObj(source)) {
        throw new IllegalArgumentException("message source is null");
    }
    if (Check.NuNStrStrict(code)) {
        throw new IllegalArgumentException("code is empty");
    }
    //如果没有object 默认设置为空数组
    if (Check.NuNObj(params)) {
        params = new Object[]{};
    }
    String message = null;

    if (Check.NuNStrStrict(defaultMsg)) {
        message = source.getMessage(code, params, Locale.SIMPLIFIED_CHINESE);
    } else {
        message = source.getMessage(code, params, defaultMsg, Locale.SIMPLIFIED_CHINESE);
    }
    if (Check.NuNStrStrict(message)) {
        throw new NumberFormatException("message is empty");
    } else {
        return Integer.valueOf(message);
    }
}
MessageSourceUtil.java 文件源码 项目:asura 阅读 34 收藏 0 点赞 0 评论 0
/**
 * 查找错误消息
 *
 * @param source
 * @param code
 * @param params
 * @param defaultMsg
 *
 * @return
 */
public static String getChinese(MessageSource source, String code, Object[] params, String defaultMsg) {
    if (Check.isNull(source)) {
        throw new IllegalArgumentException("message source is null");
    }
    if (Check.isNullOrEmpty(code)) {
        throw new IllegalArgumentException("code is empty");
    }
    //如果没有object 默认设置为空数组
    if (Check.isNull(params)) {
        params = new Object[]{};
    }
    if (Check.isNullOrEmpty(defaultMsg)) {
        return source.getMessage(code, params, Locale.SIMPLIFIED_CHINESE);
    } else {
        return source.getMessage(code, params, defaultMsg, Locale.SIMPLIFIED_CHINESE);
    }
}
MessageSourceUtil.java 文件源码 项目:asura 阅读 31 收藏 0 点赞 0 评论 0
/**
 * 查找错误消息
 *
 * @param source
 * @param code
 * @param params
 * @param defaultMsg
 *
 * @return
 */
public static int getIntMessage(MessageSource source, String code, Object[] params, String defaultMsg) {
    if (Check.isNull(source)) {
        throw new IllegalArgumentException("message source is null");
    }
    if (Check.isNullOrEmpty(code)) {
        throw new IllegalArgumentException("code is empty");
    }
    //如果没有object 默认设置为空数组
    if (Check.isNull(params)) {
        params = new Object[]{};
    }
    String message = null;

    if (Check.isNullOrEmpty(defaultMsg)) {
        message = source.getMessage(code, params, Locale.SIMPLIFIED_CHINESE);
    } else {
        message = source.getMessage(code, params, defaultMsg, Locale.SIMPLIFIED_CHINESE);
    }
    if (Check.isNullOrEmpty(message)) {
        throw new NumberFormatException("message is empty");
    } else {
        return Integer.valueOf(message);
    }
}
MailService.java 文件源码 项目:jhipster-microservices-example 阅读 31 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
MailService.java 文件源码 项目:MTC_Labrat 阅读 38 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
ErrorFieldsExceptionHandler.java 文件源码 项目:REST-Web-Services 阅读 36 收藏 0 点赞 0 评论 0
/**
 * Constructor.
 *
 * @param messageSource The message source to use
 */
@Autowired
public ErrorFieldsExceptionHandler(
        final MessageSource messageSource
) {
    this.messageSource = messageSource;
}
ThymeleafConfiguration.java 文件源码 项目:buenojo 阅读 26 收藏 0 点赞 0 评论 0
@Bean
@Description("Spring mail message resolver")
public MessageSource emailMessageSource() {
    log.info("loading non-reloadable mail messages resources");
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("classpath:/mails/messages/messages");
    messageSource.setDefaultEncoding(CharEncoding.UTF_8);
    return messageSource;
}
MessageHelper.java 文件源码 项目:NGB-master 阅读 31 收藏 0 点赞 0 评论 0
/**
 * Represents a factory method to instantiate {@code MessageHelper} singleton following by
 * "Double Checked Locking & Volatile" pattern.
 *
 * @param messageSource {@code MessageSource} used as the underlying messages bundle
 * @return {@code MessageHelper}
 */
public static MessageHelper singleton(final MessageSource messageSource) {
    MessageHelper helper = instance;
    if (helper == null) {
        synchronized (MessageHelper.class) {
            helper = instance;
            if (helper == null) {
                Assert.notNull(messageSource);
                instance = new MessageHelper(messageSource);
                helper = instance;
            }
        }
    }
    return helper;
}
MessageUtils.java 文件源码 项目:devops-cstack 阅读 29 收藏 0 点赞 0 评论 0
public static Message writeAfterThrowingApplicationMessage(Exception e,
                                                           User user, String type, MessageSource messageSource,
                                                           Locale locale) {
    Message message = new Message();
    String body = "";
    message.setType(Message.ERROR);
    message.setAuthor(user);

    switch (type) {
        case "CREATE":
            body = messageSource.getMessage("app.create.error", null, locale);
            break;
        case "UPDATE":
            body = "Error update application - " + e.getLocalizedMessage();
            break;
        case "DELETE":
            body = "Error delete application - " + e.getLocalizedMessage();
            break;
        case "START":
            body = "Error start application - " + e.getLocalizedMessage();
            break;
        case "STOP":
            body = "Error stop application - " + e.getLocalizedMessage();
            break;
        case "RESTART":
            body = "Error restart application - " + e.getLocalizedMessage();
            break;

        default:
            body = "Error : unkown error";
            break;
    }
    message.setEvent(body);

    return message;
}
MailService.java 文件源码 项目:Armory 阅读 36 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
MailService.java 文件源码 项目:Microservices-with-JHipster-and-Spring-Boot 阅读 38 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
MailService.java 文件源码 项目:patient-portal 阅读 39 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
MailService.java 文件源码 项目:TorgCRM-Server 阅读 32 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
MailConfig.java 文件源码 项目:nixmash-blog 阅读 33 收藏 0 点赞 0 评论 0
@Bean
public MessageSource mailMessageSource() {
    ResourceBundleMessageSource msgsource = new ResourceBundleMessageSource();
    if (nixmashModeEnabled)
        msgsource.setBasename("mail-nixmash");
    else
        msgsource.setBasename("mail-messages");
    return msgsource;
}
WebConfig.java 文件源码 项目:nixmash-blog 阅读 28 收藏 0 点赞 0 评论 0
@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource msgsource = new ResourceBundleMessageSource();
    if (nixmashModeEnabled)
        msgsource.setBasename("nixmash");
    else
        msgsource.setBasename("messages");

    msgsource.setUseCodeAsDefaultMessage(
            Boolean.parseBoolean(environment.getRequiredProperty(USE_CODE_AS_DEFAULT_MESSAGE)));
    return msgsource;
}
MailService.java 文件源码 项目:speakTogether 阅读 29 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
LocaleConfiguration.java 文件源码 项目:xm-ms-entity 阅读 32 收藏 0 点赞 0 评论 0
@Bean
public MessageSource defaultMessageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasenames("messages", "i18n/messages");
    messageSource.setDefaultEncoding(Charset.forName("UTF-8").name());
    messageSource.setFallbackToSystemLocale(true);
    messageSource.setCacheSeconds(-1);
    messageSource.setAlwaysUseMessageFormat(false);
    messageSource.setUseCodeAsDefaultMessage(true);
    return messageSource;
}
MailService.java 文件源码 项目:Code4Health-Platform 阅读 34 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
ClassUtilsTest.java 文件源码 项目:gemini.blueprint 阅读 34 收藏 0 点赞 0 评论 0
public void testInterfacesHierarchy() {
       //Closeable.class,
    Class<?>[] clazz = ClassUtils.getAllInterfaces(DelegatedExecutionOsgiBundleApplicationContext.class);
    Class<?>[] expected =
            { ConfigurableOsgiBundleApplicationContext.class, ConfigurableApplicationContext.class,
                    ApplicationContext.class, Lifecycle.class, Closeable.class, EnvironmentCapable.class, ListableBeanFactory.class,
                    HierarchicalBeanFactory.class, MessageSource.class, ApplicationEventPublisher.class,
                    ResourcePatternResolver.class, BeanFactory.class, ResourceLoader.class, AutoCloseable.class };

    assertTrue(compareArrays(expected, clazz));
}
MailService.java 文件源码 项目:spring-io 阅读 33 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
MailService.java 文件源码 项目:spring-io 阅读 32 收藏 0 点赞 0 评论 0
public MailService(JHipsterProperties jHipsterProperties, JavaMailSender javaMailSender,
        MessageSource messageSource, SpringTemplateEngine templateEngine) {

    this.jHipsterProperties = jHipsterProperties;
    this.javaMailSender = javaMailSender;
    this.messageSource = messageSource;
    this.templateEngine = templateEngine;
}
AbstractApplicationContext.java 文件源码 项目:lams 阅读 30 收藏 0 点赞 0 评论 0
/**
 * Initialize the MessageSource.
 * Use parent's if none defined in this context.
 */
protected void initMessageSource() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
        this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
        // Make MessageSource aware of parent MessageSource.
        if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
            HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
            if (hms.getParentMessageSource() == null) {
                // Only set parent context as parent MessageSource if no parent MessageSource
                // registered already.
                hms.setParentMessageSource(getInternalParentMessageSource());
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Using MessageSource [" + this.messageSource + "]");
        }
    }
    else {
        // Use empty MessageSource to be able to accept getMessage calls.
        DelegatingMessageSource dms = new DelegatingMessageSource();
        dms.setParentMessageSource(getInternalParentMessageSource());
        this.messageSource = dms;
        beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
                    "': using default [" + this.messageSource + "]");
        }
    }
}
AbstractApplicationContext.java 文件源码 项目:lams 阅读 29 收藏 0 点赞 0 评论 0
/**
 * Return the internal MessageSource used by the context.
 * @return the internal MessageSource (never {@code null})
 * @throws IllegalStateException if the context has not been initialized yet
 */
private MessageSource getMessageSource() throws IllegalStateException {
    if (this.messageSource == null) {
        throw new IllegalStateException("MessageSource not initialized - " +
                "call 'refresh' before accessing messages via the context: " + this);
    }
    return this.messageSource;
}
SimpleTheme.java 文件源码 项目:lams 阅读 32 收藏 0 点赞 0 评论 0
/**
 * Create a SimpleTheme.
 * @param name the name of the theme
 * @param messageSource the MessageSource that resolves theme messages
 */
public SimpleTheme(String name, MessageSource messageSource) {
    Assert.notNull(name, "Name must not be null");
    Assert.notNull(messageSource, "MessageSource must not be null");
    this.name = name;
    this.messageSource = messageSource;
}
OutputFactory.java 文件源码 项目:lams 阅读 25 收藏 0 点赞 0 评论 0
/**
    * Get the I18N description for this key. If the tool has supplied a messageService, then this is used to look up
    * the key and hence get the text. Otherwise if the tool has supplied a I18N languageFilename then it is accessed
    * via the shared toolActMessageService. If neither are supplied or the key is not found, then any "." in the name
    * are converted to space and this is used as the return value.
    *
    * This is normally used to get the description for a definition, in whic case the key should be in the format
    * output.desc.[definition name], key = definition name and addPrefix = true. For example a definition name of
    * "learner.mark" becomes output.desc.learner.mark.
    *
    * If you want to use this to get an arbitrary string from the I18N files, then set addPrefix = false and the
    * output.desc will not be added to the beginning.
    */
   protected String getI18NText(String key, boolean addPrefix) {
String translatedText = null;

MessageSource tmpMsgSource = getMsgSource();
if (tmpMsgSource != null) {
    if (addPrefix) {
    key = KEY_PREFIX + key;
    }
    Locale locale = LocaleContextHolder.getLocale();
    try {
    translatedText = tmpMsgSource.getMessage(key, null, locale);
    } catch (NoSuchMessageException e) {
    log.warn("Unable to internationalise the text for key " + key
        + " as no matching key found in the msgSource");
    }
} else {
    log.warn("Unable to internationalise the text for key " + key
        + " as no matching key found in the msgSource. The tool's OutputDefinition factory needs to set either (a) messageSource or (b) loadedMessageSourceService and languageFilename.");
}

if (translatedText == null || translatedText.length() == 0) {
    translatedText = key.replace('.', ' ');
}

return translatedText;
   }


问题


面经


文章

微信
公众号

扫码关注公众号