作用是什么?
使用@ImportResource是为了导入Spring的XML配置文件
区别是什么?
@Import注解是为了引入JAVA类
SpringBoot - @Import注解使用详解
@PropertySource是为了引入自定义的配置文件(*.properties),从而达到配置文件与实体属性的映射
SpringBoot - @PropertySource注解使用详解
场景是什么?
①。 有一个遗留的XML文件, 比如名叫 my-beans.xml 里面有很多的配置Bean, 都需要注册到Spring容器中, 让容器来管理这些Bean; ②。 传统的SpringMVC的项目, 原来的XML配置文件不想删除, 但又想用SpringBoot框架来改造它。遇到以上两种场景,就可以使用@ImportResource来导入外部资源到Spring容器中。
如何使用?
启动类上加注解:
@ImportResource({"classpath*:XML配合文件的名字"})
@ImportResource(locations = {"classpath:XML配合文件的名字"})
@ImportResource(locations = {"classpath:config/mallx.xml"})
@SpringBootApplication
@EnableSwagger2
@ImportResource({"classpath*:product.xml"})
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class, args);
}
}