本篇我们一起来看一下SpringBoot为什么要新建MessageSource Bean?这里我总结了以下内容:
* AbstractApplicationContext 的实现决定了 MessageSource 內建实现
* Spring Boot 通过外部化配置简化 MessageSource Bean 构建
* Spring Boot 基于 Bean Validation 校验非常普遍
接下来我们就通过实战的方式一起实践一下。
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eleven.thinking.in.spring.i18n;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
/**
* Spring Boot 场景下自定义 {@link MessageSource} Bean
*
* @author <a href="mailto: eleven.hm@vip.163.com">eleven</a>
* @see MessageSource
* @see MessageSourceAutoConfiguration
* @see ReloadableResourceBundleMessageSource
* @since
*/
@EnableAutoConfiguration
public class CustomizedMessageSourceBeanDemo { // @Configuration Class
/**
* 在 Spring Boot 场景中,Primary Configuration Sources(Classes) 高于 *AutoConfiguration
* @return
*/
@Bean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)
public MessageSource messageSource() {
return new ReloadableResourceBundleMessageSource();
}
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext =
// Primary Configuration Class
new SpringApplicationBuilder(CustomizedMessageSourceBeanDemo.class)
.web(WebApplicationType.NONE)
.run(args);
ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
if (beanFactory.containsBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)) {
// 查找 MessageSource 的 BeanDefinition
System.out.println(beanFactory.getBeanDefinition(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME));
// 查找 MessageSource Bean
MessageSource messageSource = applicationContext.getBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
System.out.println(messageSource);
}
// 关闭应用上下文
applicationContext.close();
}
}
本文暂时没有评论,来添加一个吧(●'◡'●)