@ImportResource
**作用:**导入spring中的xml配置文件,使得配置中的文件生效
例子代码如下:
@ImportResource(locations = "classpath:test.xml")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo.pojo;
public class People {
private String Pname;
private int age;
public String getPname() {
return Pname;
}
public void setPname(String pname) {
Pname = pname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.example.demo.controller;
import com.example.demo.pojo.People;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ControllerOne {
@Autowired
People p;
@RequestMapping("/one")
public People getP(){
return p;
}
}
【注】 xml文件放置的位置,之后代码在不同服务器上运行时,必须使用classpath,否则无法找到文件位置
结果: