前言

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. esp32 Guru Meditation 错误解决方案(转)

    Guru Meditation本节将对打印在 Guru Meditation Error: Core panic'ed后面括号中的致错原因进行逐一解释.IllegalInstruction此 CPU ...

  2. IBM刀箱服务器的SW

    刀箱交换机说明: 1.刀箱交换机可以看到的24个口都是ext端口,其中因为授权原因,只激活了前10个端口. 2.交换机配置中的inta端口为服务器直接连接的端口,inta1-inta14,这些都是对应 ...

  3. Python使用笔记001

    一.Pycharm小技巧 1.pycharm创建项目时,选择Python环境,不使用默认的虚拟环境 2.如何在pycharm中查看python版本 Files--Settings--Project I ...

  4. C语言:监听键盘

    所谓键盘监听,就是用户按下某个键时系统做出相应的处理,本章讲到的输入输出函数也是键盘监听函数的一种,例如 getchar().getche().getch() 等.下面的代码演示了 getche() ...

  5. navicate for mysql命令中输入中文报错

    insert into xsxx(name,xb) values('李四','男') 错误提示: [SQL]insert into xsxx3(name,xb) values('李四','男') [E ...

  6. 团队开发day10

    项目整合成功,测试功能基本达到,目标完成!

  7. 正则表达式的模式匹配----V客学院技术分享

    正则表达式是由一个字符序列形成的搜索模式. 你在文本中搜索数据时,你可以用搜索模式来描述你要查询的内容. 正则表达式可以是一个简单的字符,或一个更复杂的模式. 正则表达式可用于所有文本搜索和文本替换的 ...

  8. debian9 python环境设置

    file /usr/bin/python which python2 which python3 mv /usr/bin/python /usr/bin/python_bk ln -s /usr/bi ...

  9. 2021 NOI冬令营

    2021 NOI冬令营 -- 本来想写得稍微文艺一点的,但是停课这么久已经退化到不会写文章了. 毕竟省选了我才来补的,时间也过去很久了. 毕竟这次我没有获得任何奖项. 我唯一的获得就是-- --好好看 ...

  10. P5042 丢失的题面

    P5042 丢失的题面 顺序:10 - 1 - 7 - 8 - 9 - 4 - 5 - 6 - 2 - 3 Point 10 读入,特判,输出. 读入的英文意思是让选手输出自己的程序本身,这个题的确存 ...