springBoot2.0使用@ImportResource引入spring配置文件.xml
1. 编写spring配置文件.xml
这里是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="dog" class="cn.jfjb.bean.Dog">
<property name="name" value="jjj"></property>
<property name="age" value="1233"></property>
</bean>
</beans>
bean文件
package cn.jfjb.bean;
/**
* @author john
* @date 2019/11/22 - 8:56
*/
public class Dog {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2. 在启动类中使用@ImportResource引入xml资源
package cn.jfjb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(locations = {"classpath:bean.xml"})
public class SbHelloworld01QuickApplication {
public static void main(String[] args) {
SpringApplication.run(SbHelloworld01QuickApplication.class, args);
}
}
3. 测试
package cn.jfjb.sbhelloworld01quick;
import cn.jfjb.bean.Dog;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
@SpringBootTest
class SbHelloworld01QuickApplicationTests {
@Autowired
ApplicationContext ioc;
@Autowired
Dog dog;
@Test
void testImportResource(){
System.out.println(ioc.containsBean("dog"));
System.out.println(dog);
}
}