SpringBoot - @ImportResource注解使用详解

作用是什么?

使用@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);
   }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloneme01

谢谢您的支持与鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值