测试类:
Service类:
public class HelloService {
public String gethello(){
return "hello xml";
}
}
Controller类:
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public void getHello(){
System.out.println(helloService.gethello());
}
}
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 class="com.dong.xml.HelloService" id="HelloController"/>
</beans>
至关重要的是启动类,只需要在启动类上加上@ImportResource注解,并标明位置,就成功配置了
@SpringBootApplication
@ImportResource("classpath:beans.xml")
public class XmlApplication {
public static void main(String[] args) {
SpringApplication.run(XmlApplication.class, args);
}
}