@ImportResource:导入Spring的配置文件,让配置文件里面功能生效;想让Spring的配置文件生效,加载进来,@ImportResource标注在一个配置类上。
@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class SpringBoot02ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
}
}
配置文件的bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.atguigu.springboot.service.HelloService"></bean>
</beans>
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
ApplicationContext ioc;
//判断容器中是否有helloService实现类
@Test
public void testHelloService(){
boolean b=ioc.containsBean("helloService");
System.out.println("容器中是否含有bean:"+b);
}
}
运行结果: