前言

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

五.成功

@ImportResource-SpringBoot使用xml配置Bean的更多相关文章

  1. 【小家Spring】老项目迁移问题:@ImportResource导入的xml配置里的Bean能够使用@PropertySource导入的属性值吗?

    #### 每篇一句 > 大师都是偏执的,偏执才能产生力量,妥协是没有力量的.你对全世界妥协了你就是空气.所以若没有偏见,哪来的大师呢 #### 相关阅读 [[小家Spring]详解Propert ...

  2. @ImportResource导入的xml配置里的Bean能够使用@PropertySource导入的属性值吗?

    每篇一句 大师都是偏执的,偏执才能产生力量,妥协是没有力量的.你对全世界妥协了你就是空气.所以若没有偏见,哪来的大师呢 相关阅读 [小家Spring]详解PropertyPlaceholderConf ...

  3. springboot中xml配置之@ImportResource

    springboot中进行相关的配置往往有java配置和xml配置两种方式. 使用java的方式配置只需要使用@configuration注解即可,而使用xml的方式配置的话需要使用@ImportRe ...

  4. SpringBoot零XML配置的Spring Boot Application

    Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性properties文件.YAML 文件.环境变量和命令行参数来定义优先级不同的配置值.零XML配置的Spring B ...

  5. SpringBoot无XML配置

    SpringBoot,自己研究了好几天,以前也是没有接触过这类的框架,不过原理吧,也就是那么些个原理,毕竟都是Spring开源下的子框架. 好了,回归正题,今天晚上研究了好久,写出来了无配置文件的ja ...

  6. 5. Spring 通过 XML 配置 bean (进阶)

    1. 设置 bean 的作用域 当通过 Spring IOC 容器创建 bean 实例的时候,不仅可以完成 bean 的实例化,也可以为 bean 指定特定的作用域,Spring 支持以下 5 种作用 ...

  7. spring Ioc容器之使用XML配置Bean

    1.项目截图 2.创建xml文件 3.打印机接口 package com.example.demo.computerTest; public interface Printer { void init ...

  8. Spring学习之xml配置Bean总结

    学习Spring时,我用的是Maven来管理jar包,先看看maven的pom.xml: pom.xml <project xmlns="http://maven.apache.org ...

  9. Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式

    配置Bean的形式可以基于XML文件的方式,也可以基于注解的方式,而Bean的配置方式可以通过全类名(反射),通过工厂方式和FactoryBean. XML形式 <?xml version=&q ...

随机推荐

  1. Java on Visual Studio Code的更新 – 2021年6月

    Nick Zhu from Microsoft VS Code Java Team 大家好,欢迎来到 Visual Studio Code Java 更新的特别年中版.作为这篇文章的重点,我们将看看接 ...

  2. PYTHON 得到ADB的输出结果

    #利用ADB DEVICES结果判断指定手机是否正常连接,如果为offline,则adb disconnect sjh:adb connect sjh#如果没有,则执行adb connect sjhd ...

  3. python 最大公约数 最小公倍数

    def gongyueshu(m,n): if m<n: m,n=n,m elif m==n: return m if m/n==int(m/n): return n else: for i i ...

  4. matlab——插值与拟合

    @ 目录 前言 一.拟合 1.定义 2.三种判别准则 3.最小二乘法 (1)一般形式 (2)常用函数 (3)matlab实现 二.插值 1.定义 2.方法 (1)分段线性插值 (2)拉格朗日插值多项式 ...

  5. Oracle执行计划总结

    一.ORACLE中常见执行计划 表访问的执行计划 1.table access full:全表扫描.它会访问表中的每一条记录. 2.table access by user rowid:输入源rowi ...

  6. 未开通js之前的纯css网页主题

    本主题修改自其他大佬:Rocket1184/MaterialCnblogs: Material Theme for cnblogs.com (github.com) 只需在博客后台选择"禁用 ...

  7. Xshell、winscp连不上Linux虚拟机

    1.环境 本地机器WIN7环境,使用VMware Workstation Pro安装的CentOS7,系统镜像CentOS-6.1-x86_64-netinstall.iso 2.问题与分析 我的虚拟 ...

  8. 微信小程序云开发-云存储-上传文件(图片/视频)到云存储 精简代码

    说明 图片/视频这类文件是从客户端会话选择文件. 一.wxml文件添加if切换显示 <!--上传文件到云存储--> <button bindtap="chooseImg&q ...

  9. jumpserver 用户,系统用户和管理用户 普通用户和特权用户 区别

    前言 现在很多公司都有在用Jumpserver跳板机 有很多人一直对jumpserver的各种用户还不是很了解 当你了解了这几个概念了之后,就能更好的灵活的运用到分配权限当中去. 下面我们一个一个的说 ...

  10. centos7下安装、配置Nginx、设置Nginx开机自启动

    测试环境: [root@centos-linux ~]# cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core) [root@cento ...