你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

8.容器功能-ImportResource-原生配置文件引入

2022/9/3 3:32:05

在xml配置文件中配置的bean 在springboot中是无法正常使用的,因为用的不是这个方法了

ApplicationContext ctx= new ClassPathXmlApplicationContext("bean.xml");

在springboot中用的
SpringApplication.run(App.class, args);

 

要中springboot中使用原来的xml配置的bean 就需要使用@ImportResource 

@ImportResource("classpath:beans.xml") //
public class MyConfig {}

======================测试=================
        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("haha:"+haha);//true
        System.out.println("hehe:"+hehe);//true

 

@ImportResource("classpath:beans.xml") 中配置类上 加上这个注解 指定spring的xml 就自动加入容器了