国际化

先定义一个MessageSource:

  1. @Bean
  2. public MessageSource messageSource() {
  3. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  4. messageSource.setBasename("messages");
  5. return messageSource;
  6. }

有了这个Bean,你可以在你任意想要进行国际化的地方使用该MessageSource。
同时,因为ApplicationContext也拥有国家化的功能,所以可以直接这么用:

  1. annotationConfigApplicationContext.getMessage("test", null, new Locale("en_CN"))

资源加载

ApplicationContext还拥有资源加载的功能,比如,可以直接利用ApplicationContext获取某个文件的内容:

  1. Resource resource = annotationConfigApplicationContext.getResource("file://D:\\IdeaProjects\\spring-framework\\luban\\src\\main\\java\\com\\luban\\entity\\User.java");
  2. System.out.println(resource.contentLength());

你可以想想,如果你不使用ApplicationContext,而是自己来实现这个功能,就比较费时间了。

还比如你可以:

  1. Resource resource = annotationConfigApplicationContext.getResource("classpath:com/luban/entity/User.class");
  2. System.out.println(resource.contentLength());

还可以一次性获取多个:

  1. Resource[] resources = annotationConfigApplicationContext.getResources("classpath:com/luban/service/*.class");
  2. for (Resource resource : resources) {
  3. System.out.println(resource.contentLength());
  4. }

这个功能用到了策略模式

获取运行时环境

  1. // 获取JVM所允许的操作系统的环境
  2. annotationConfigApplicationContext.getEnvironment().getSystemEnvironment();
  3. // 获取JVM本身的一些属性,包括-D所设置的
  4. annotationConfigApplicationContext.getEnvironment().getSystemProperties();
  5. // 还可以直接获取某个环境或properties文件中的属性
  6. annotationConfigApplicationContext.getEnvironment().getProperty("lubanyyy")

注意,可以利用

  1. @PropertySource("classpath:application.properties")

来使得某个properties文件中的参数添加到运行时环境中

事件发布

先定义一个事件监听器

  1. @Bean
  2. public ApplicationListener applicationListener() {
  3. return new ApplicationListener() {
  4. @Override
  5. public void onApplicationEvent(ApplicationEvent event) {
  6. System.out.println("接收到了一个事件");
  7. }
  8. };
  9. }

然后发布一个事件:

  1. annotationConfigApplicationContext.publishEvent("kkk");

类型转化

PropertyEditor

JDK中提供的类型转化工具类

  1. public class StringToUserPropertyEditor extends PropertyEditorSupport implements PropertyEditor {
  2. @Override
  3. public void setAsText(String text) throws IllegalArgumentException {
  4. User user = new User();
  5. user.setName(text);
  6. this.setValue(user);
  7. }
  8. }
  1. StringToUserPropertyEditor propertyEditor = new StringToUserPropertyEditor();
  2. propertyEditor.setAsText("1");
  3. User value = (User) propertyEditor.getValue();
  4. System.out.println(value);

如何向Spring中注册PropertyEditor:

  1. @Bean
  2. public CustomEditorConfigurer customEditorConfigurer() {
  3. CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
  4. Map<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();
  5. propertyEditorMap.put(User.class, StringToUserPropertyEditor.class);
  6. customEditorConfigurer.setCustomEditors(propertyEditorMap);
  7. return customEditorConfigurer;
  8. }

假设现在有如下Bean:

  1. @Component
  2. public class UserService {
  3. @Value("true")
  4. User test;
  5. public void test() {
  6. System.out.println(test);
  7. }
  8. }

那么test属性就能正常的完成属性赋值

ConversionService

Spring中提供的类型转化服务,它比PropertyEditor更强大

  1. public class StringToUserConverter implements ConditionalGenericConverter {
  2. @Override
  3. public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  4. return sourceType.getType().equals(String.class) && targetType.getType().equals(User.class);
  5. }
  6. @Override
  7. public Set<ConvertiblePair> getConvertibleTypes() {
  8. return Collections.singleton(new ConvertiblePair(String.class, User.class));
  9. }
  10. @Override
  11. public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  12. User user = new User();
  13. user.setName((String)source);
  14. return user;
  15. }
  16. }
  1. DefaultConversionService conversionService = new DefaultConversionService();
  2. conversionService.addConverter(new StringToUserConverter());
  3. User value = conversionService.convert("1", User.class);
  4. System.out.println(value);

如何向Spring中注册ConversionService:

  1. @Bean
  2. public ConversionServiceFactoryBean conversionService() {
  3. ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
  4. conversionServiceFactoryBean.setConverters(Collections.singleton(new StringToUserConverter()));
  5. return conversionServiceFactoryBean;
  6. }

TypeConverter

整合了PropertyEditor和ConversionService的功能,是Spring内部用的

  1. SimpleTypeConverter typeConverter = new SimpleTypeConverter();
  2. typeConverter.registerCustomEditor(User.class, new StringToUserPropertyEditor());
  3. //typeConverter.setConversionService(conversionService);
  4. User value = typeConverter.convertIfNecessary("1", User.class);
  5. System.out.println(value);