Spring初始化默认转换器

UML
这里写图片描述
这里写图片描述
关键代码

1、启动spring

private static void base() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[] { "classpath:applicationContext.xml" });
        context.start();
    }

2、ClassPathXmlApplicationContext中

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
            throws BeansException {

        super(parent);
        //设置默认的环境(包括默认的转换器及对路径表达式的解析)
        setConfigLocations(configLocations);
        if (refresh) {
            refresh();
        }
    }

3、AbstractRefreshableConfigApplicationContext中

protected String resolvePath(String path) {
        //获取默认环境配置及转换路径中的表达式
        return getEnvironment().resolveRequiredPlaceholders(path);
    }

4、DefaultConversionService类中

public static void addDefaultConverters(ConverterRegistry converterRegistry) {
        addScalarConverters(converterRegistry);
        addCollectionConverters(converterRegistry);

        converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry));
        if (jsr310Available) {
            Jsr310ConverterRegistrar.registerJsr310Converters(converterRegistry);
        }

        converterRegistry.addConverter(new ObjectToObjectConverter());
        converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry));
        converterRegistry.addConverter(new FallbackObjectToStringConverter());
        if (javaUtilOptionalClassAvailable) {
            converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService) converterRegistry));
        }
    }

猜你喜欢

转载自blog.csdn.net/q563573095/article/details/80138215
今日推荐