今天看SpringMVC中Dispatcher源代码发现了一个static关键词的用法,因为在java中会先去加载静态变量,静态代码块,再去加载非静态变量,普通代码块。所以dispatcher中有一段static {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'DispatcherServlet.properties': " + ex.getMessage());
}
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
try {
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load 'DispatcherServlet.properties': " + ex.getMessage());
}
}
去加载默认的配置。这样就可以做到,在程序启动时先去加载默认的配置,然后调用onRefresh(ApplicationContext context)方法去调用相应的方法加载自己配置的组件覆盖默认配置。