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

在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 就自动加入容器了

posted @ 2022-09-03 03:09  咖喱给给啊  阅读(40)  评论(0)    收藏  举报