@ImportResourceSpringBoot使用xml配置Bean

编程

# 前言
  SpringBoot推荐使用注解的方式去声明bean,但还是提供了xml的方式去加载bean

# 一.创建要声明为bean的实体类
![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1lOTcwZmU0M2E0OTdjY2YwLnBuZw?x-oss-process=image/format,png)

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容器

![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1hOGM3NDZiYzdkYmQ0ODRmLnBuZw?x-oss-process=image/format,png)

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
![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1lN2QwZGNmYjczNDNiODY2LnBuZw?x-oss-process=image/format,png)

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();
    }

}

```

# 五.成功
![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1hYzczOGQ4ZWY3YWI5MzZlLnBuZw?x-oss-process=image/format,png)


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


![image.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy01MmFlMzg1NWUyMzMyZDAyLnBuZw?x-oss-process=image/format,png)


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


![微信图片_20190614200326.jpg](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy03NjJmYTFlOGFlOWZjMjk3LmpwZw?x-oss-process=image/format,png)


我的星球欢迎加入:
![微信图片_20191210093234.jpg](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1mYTlmMWExMjkwYTExMjhkLmpwZw?x-oss-process=image/format,png)

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

![微信图片_20191111194455.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xODU1NzgxMy1iZjlkMjQzYjg5OWIyYTA1LnBuZw?x-oss-process=image/format,png)

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

回到顶部