Spring重要特性:IOC/DI (控制反转 / 依赖注入)

传统方式

使用beans.xml

  1. 创建bean类
package com.example.annotations.bean;public class Person {private String name;private Integer age;public Person() {}public Person(String name, Integer age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +'}';}
}
  1. idea中创建beans.xml
    new – xml configuration file – Spring config
<bean id="person" class="com.example.annotations.bean.Person"><property name="name" value="lxl"/><property name="age" value="21"/>
</bean>
  1. 测试
    @Testvoid beanTest(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");Person person = (Person) applicationContext.getBean("person");System.out.println(person);}

注解方式 @Configuration @Bean

  1. 编写MainConfig类:配置类等同于beans.xml

    package com.example.annotations.config;import com.example.annotations.bean.Person;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;// @Configuration : 告诉Spring这是个配置类
    @Configuration
    public class MainConfig {//给容器注入一个bean,类型为返回值的类型,id默认使用方法的id@Bean("person")public Person getPerson(){return new Person("qd",21);}
    }
    
  2. 测试

        @Testvoid beanConfigTest(){ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);Person person = applicationContext.getBean(Person.class);//获取对应类型组件在IOC容器中的名字 返回一个数组String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
    //        System.out.println(Arrays.toString(beanNamesForType));for (String str:beanNamesForType){System.out.println("toString:" + str.toString());}}
    
  3. Notes

    • @Configuration:告诉Spring这是个配置类
    • @Bean:给容器注入一个bean
      • 标注在类上:类型为bean类的类型,id默认为类的小写
      • 标注在方法上:类型为返回值的类型,id默认使用方法的id

【SpringBoot_ANNOTATIONS】组件注册 01 @Configuration @Bean相关推荐

  1. 2、组件注册-@Configuration@Bean给容器中注册组件

    2.组件注册-@Configuration&@Bean给容器中注册组件 2.1 创建maven项目 spring-annotation pom.xml文件添加 spring-context 依 ...

  2. spring5学习系列之------1 给容器注册组件一 @Configuration  @Bean  @Scope  @Lazy

    在工作中我们对spring已经很常用了,但是用的大部分都是spring一些很少的部分,该系列是基于spring5版本的,先介绍相关组件的内容,在涉及到源码内容.话不多说,直接上干货吧 常用注解之给容器 ...

  3. spring系列-注解驱动原理及源码-bean组件注册

    目录 一.环境初始化 1.环境准备 二.bean的手动注入 1.xml方式注入bean 2.使用@Configuration&@Bean方式注入bean 三.自动扫描注册组件及bean 1.使 ...

  4. Spring注解开发系列Ⅰ--- 组件注册(上)

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文 ...

  5. spring注解--组件注册

    组件注册 1.@Configuration , @Bean 1)xml方式 <?xml version="1.0" encoding="UTF-8"?&g ...

  6. 【转】Castle Windsor之组件注册

    [转]Castle Windsor之组件注册 注册方式较多,大体有这么几种,学习得比较粗浅,先记录: 1.逐个注册组件 即对每个接口通过代码指定其实现类,代码: container.Register( ...

  7. (24)Vue.js组件—组件注册

    一.Vue.js组件注册内容 • 全局注册 • 组件基础 • 局部注册 二.全局注册 • 全局注册的组件在注册后可以用于任意实例或组件中. <body><div id="a ...

  8. oneos组件系列01:i2c_tools

    oneos组件系列01:i2c_tools 灵感来源: Arduino库i2cdetect rt-i2c-tools软件包 本组件仓库: https://gitee.com/dreamfly233/O ...

  9. 软件框架SpringBoot-实现使用@Component@Data@Configuration@Bean(配置类控制类实体类)等方法实现将配置文件从8080端口显示在网页上

    一.前言 1.该程序代码是使用idea2021.12版本编写的,若使用其他软件请对照好配置: 2.这个程序具体的内容我忘了,只知道使用@Component@ConfigurationPropertie ...

  10. 【问题集萃】N005:SpringBoot2启动提示组件注册失败 Field userService XXXX that could not be found.

    SpringBoot2启动提示组件注册失败Field userService XXXX that could not be found. Field userInfoService in com.ga ...

最新文章

  1. 2022-2028年中国重油行业市场研究及前瞻分析报告
  2. “630”后逆变器售价下滑 企业如何应战?
  3. jquery 移除border_jQuery - 删除元素
  4. JAVA——Windows双击jar包为可执行操作
  5. 从前馈到反馈:解析循环神经网络(RNN)及其tricks
  6. php mysql_fetch_array 不要0_1_php – mysql_fetch_array不适用于1行查询结果
  7. python创建列表副本_Python编程15:Python列表的排序和列表的副本
  8. 大公司都有哪些开源项目~~~简化版
  9. Delphi中用Sender参数实现代码重用
  10. StreamInsight 编程模型之适配器
  11. win10 安装pytorch gpu 及 解决报错“OSError: [WinError 126] 找不到指定的模块
  12. 岁月温柔-23 妈妈转到省医院第3天 突然icu有床位了
  13. CentOS8.3安装时安装源设置基础软件仓库时出错
  14. 洛谷 P3356 火星探险问题
  15. Mac下浏览器超实用快捷键
  16. 微软word如何插入页码_如何在Microsoft Word中插入,删除和管理超链接
  17. Xftp 传文件到虚拟机一直显示状态错误,传不进去
  18. WIN10下DOCKER的安装与使用
  19. 「PS-CC2019新版教程」画板工具-基础篇
  20. java常微分方程数值解,SymPy / SciPy:求解具有不同变量的常微分方程组

热门文章

  1. 解读畅捷通微服务治理能力提升之路
  2. 阿里张勇《人民日报》刊发署名文章:抓住数字新基建的机遇
  3. Pouch Container与Kubernetes相比效率如何?
  4. 我与OTC的诸位大神
  5. 【图像隐写】基于matlab DCT图像隐写【含Matlab源码 365期】
  6. 【优化调度】基于matlab粒子群算法求解燃机冷热电优化联供问题【含Matlab源码 330期】
  7. 月球 dem_通过“月球灾害”应对错误信息的流行
  8. android gsm编程,Android新手入门 FAQ
  9. 天文软件ds9对图片调整颜色和两张图片坐标自动匹对
  10. mysql workbench首页_MySQL Workbench是干什么的?