@ImportResourceSpringBoot使用xml配置Bean
# 前言
SpringBoot推荐使用注解的方式去声明bean,但还是提供了xml的方式去加载bean
# 一.创建要声明为bean的实体类

WzqEntity.java
```
package com;
/**
* @description:
* @author: Wzq
* @create: 2019-12-12 15:44
*/
public class WzqEntity {
public String getStr(){
return "我是xml配置的bean!";
}
}
```
# 二.在resources下创建spring-bean.xml
spring-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="wzqEntity" class="com.WzqEntity" ></bean>
</beans>
```
# 三.使用@ImportResource注解将xml配置bean加入spring ioc容器

config.java
```
package com.wzq.dome.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* @description:
* @author: Wzq
* @create: 2019-12-12 15:48
*/
@Configuration
@ImportResource(value = {"classpath:spring-bean.xml"})
public class config {
}
```
# 四.使用xml声明的bean

TestController.java
```
package com.wzq.dome.action;
import com.WzqEntity;
import com.wzq.dome.entity.ProFileEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description:
* @author: Wzq
* @create: 2019-12-12 10:27
*/
@RestController
public class TestController {
@Autowired
WzqEntity wzqEntity;
@RequestMapping("/test")
public String test(){
return wzqEntity.getStr();
}
}
```
# 五.成功

这是我的公众号 有最新的it咨询,和个人工作的记录:

这是我的个人微信遇到问题欢迎,提问:

我的星球欢迎加入:

最后加上高质量的淘宝店:如有质量问题随时滴滴我,童叟无欺!

以上是 @ImportResourceSpringBoot使用xml配置Bean 的全部内容, 来源链接: utcz.com/z/511700.html