老项目想用 Spring Boot 框架,但还有大量的 XML 配置文件,怎么处理?
仅用于自我学习记录。
答案就是:@ImportResource 注解,来看下它的源码:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface ImportResource {
@AliasFor("locations")
String[] value() default {};
@AliasFor("value")
String[] locations() default {};
Class<? extends BeanDefinitionReader> reader() default BeanDefinitionReader.class;
}
@ImportResource 可以指定要导入的 XML 配置文件的路径,还能指定自定义的 BeanDefinitionReader。
来一个示例,导入一个类路径下的 XML 配置文件:
@Configuration
@ImportResource("classpath:config/spring-mvc.xml")
public class MainConfiguration {
// ...
}