一、基本类型注入

1、set注入,前提是类中声明了属性的set方法 使用\
2、有参构造器注入,前提是类中声明了有参构造器 使用<constructor-arg name=“参数名” value=“参数值”>
3、注入含特殊符号值,使用<![CDATA[<<南京>>]]> 实际值为<<南京>>
4、p标签注入,需引入p名称空间 xmlns:p=“http://www.springframework.org/schema/p”

<!--配置User对象创建-->
<bean id="user" class="spring5.User"></bean><!--set方法注入属性-->
<bean id="book" class="spring5.Book"><!--使用property完成属性注入--><property name="name" value="易筋经"></property><property name="author" value="达摩老祖"></property>
</bean><!--有参构造器注入属性-->
<bean id="orders" class="spring5.Orders"><constructor-arg name="name" value="北京"></constructor-arg><constructor-arg name="address" value="中国"></constructor-arg>
</bean><!--p名称空间注入-->
<bean id="book" class="spring5.Book" p:name="九阳生工" p:author="无名氏">
</bean><!--null值注入-->
<bean id="book" class="spring5.Book"><property name="name" value="helloWord"></property><!--注入null值--><property name="author"><null/></property>
</bean><!--属性值包含特殊符号-->
<bean id="book" class="spring5.Book"><property name="author"><value><![CDATA[<<南京>>]]></value></property><property name="name" value="asd"></property>
</bean>

二、对象类型注入

方式一:外部bean。创建对象,然后在property 标签中使用ref属性将其注入:<property name=“userDao” ref=“userDaoImpl”>
例如:

<!--1、dao对象--><bean id="userDaoImpl" class="spring5.dao.UserDaoImpl"></bean><bean id="userService" class="spring5.service.UserService">
<!-- 注入userDao对象--><property name="userDao" ref="userDaoImpl"></property></bean>

方式二:内部bean,创建对象时在内部指定其属性对应的对象

<!--内部bean-->
<bean id="emp" class="com.atguigu.spring5.bean.Emp"><!--普通类型属性--><property name="ename" value="lucy"></property><property name="gender" value="女"></property><!--对象类型属性--><property name="dept.dname" value="技术"></property>
</bean>

三、数组、list、set、map、类型的属性注入,分别对应<array>或<list>、<list>或<array>、<map>、<set>

1、基础类型

<bean id="stu" class="spring5.collectiontype.Stu"><!--数组类型属性注入--><property name="courses"><array><value>java</value><value>mysql</value></array></property><!--list类型属性注入--><property name="list"><list><value>vue</value><value>js</value></list></property><!--map类型属性注入--><property name="maps"><map><entry key="one" value="python"></entry><entry key="two" value="java"></entry></map></property><!--set类型属性注入--><property name="sets"><set><value>html</value><value>css</value></set></property>
</bean>

2、对象类型
首先声明其属性对应的对象,再将其进行注入

<bean id="course01" class=".spring5.collectiontype.Course"><property name="cname" value="java"></property>
</bean>
<bean id="course02" class="spring5.collectiontype.Course"><property name="cname" value="vue"></property>
</bean>
<bean id="course03" class="spring5.collectiontype.Course"><property name="cname" value="mysql"></property>
</bean>
<bean id="stu" class="spring5.collectiontype.Stu01">
<!--        数组类型属性注入--><property name="courses"><array><ref bean="course01"></ref><ref bean="course02"></ref><ref bean="course03"></ref></array></property>
<!--        list类型属性注入--><property name="list"><list><ref bean="course02"></ref><ref bean="course03"></ref><ref bean="course01"></ref></list></property>
<!--        map类型属性注入--><property name="maps"><map><entry key="1" value-ref="course01"></entry><entry key="1" value-ref="course03"></entry><entry key="1" value-ref="course02"></entry></map></property>
<!--        set类型属性注入--><property name="sets"><set><ref bean="course02"></ref><ref bean="course03"></ref><ref bean="course01"></ref></set></property>
</bean>

3、util标签注入
首先引入util名称空间
xmlns:util=“http://www.springframework.org/schema/util”
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd

<!--基本数据类型 使用value-->
<util:list id="course"><value>vue</value><value>mysql</value><value>java</value>
</util:list>
<!--注入-->
<bean id="stu" class="spring5.collectiontype.Stu"><property name="list" ref="course"></property>
</bean>
<bean id="course1" class="spring5.collectiontype.Course"><property name="cname" value="java"></property>
</bean>
<bean id="course2" class="spring5.collectiontype.Course"><property name="cname" value="mysql"></property>
</bean>
<bean id="course3" class="spring5.collectiontype.Course"><property name="cname" value="vue"></property>
</bean>
<!--基本数据类型 使用ref将数组或集合中-->
<util:list id="coures1"><ref bean="course1"></ref><ref bean="course2"></ref><ref bean="course3"></ref>
</util:list>
<!--注入-->
<bean id="stu1" class="spring5.collectiontype.Stu01"><property name="list" ref="coures1"></property>
</bean>

获取bean

ApplicationContext context = new ClassPathXmlApplicationContext(“对应的xml文件”);
对象 名称 = beanFactory.getBean(“bean id”, 对象.class);

BeanFactory beanFactory = new ClassPathXmlApplicationContext(“对应的xml文件”);
对象 名称= beanFactory.getBean(“bean id”, 对象.class);
例如

ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user = context .getBean("user", User.class);BeanFactory beanFactory = new ClassPathXmlApplicationContext("bean1.xml");
User user = beanFactory.getBean("user", User.class);

spring基础——普通bean xml注入相关推荐

  1. Spring常见错误 - Bean构造注入报空指针异常

    Spring常见错误 - Bean构造注入报空指针异常 前言 一. 构造器内报NPE 1.1 案例 1.2 原理分析 1.2.1 空指针发生在哪一个阶段? 1.2.2 studentService字段 ...

  2. Spring基础—装配bean(一)

    在讲解Spring配置beans之前首先想一下一部成功的电影都需要哪些成员参与.首先,最重要的是要有导演.编剧.演员和投资人:其次,还有没那么明显的成员,音乐人.特技演员和艺术指导:此外,还有其他很重 ...

  3. 零配置 之 Spring 注解实现Bean依赖注入

    转载自  [第十二章]零配置 之 12.2 注解实现Bean依赖注入 --跟我学spring3 12.2  注解实现Bean依赖注入 12.2.1  概述 注解实现Bean配置主要用来进行如依赖注入. ...

  4. Spring 注解实现Bean依赖注入之@Qualifier

    三.@Qualifier:限定描述符,用于细粒度选择候选者: @Autowired默认是根据类型进行注入的,因此如果有多个类型一样的Bean候选者,则需要限定其中一个候选者,否则将抛出异常 @Qual ...

  5. Spring受管Bean依赖注入(设值注入)

    设值注入是Spring支持的多种依赖注入类型中的一种,也是最为常见的一种.设值(setter)注入指在通过调用无参构造器(或无参静态工厂方法,或工厂Bean的蜚静态工厂方法)实例化受管Bean后调用s ...

  6. spring基础——<bean>scope属性

    通过对scope属性的设置可实现单例或多例,默认不设置为单例 singleton 单例(默认值) prototype 多例 例如 <bean id="user" class= ...

  7. spring如何排除bean的注入

    文章目录 前言 常见方法 代码示例 1.排除指定注解的类-annotation 2.排查指定切面的类-aspectj 3.排除继承至某接口或类的类-assignable 4.排除符合指定匹配规则的类- ...

  8. springboot项目中的注解 启动项目的方式 解决spring的bean.xml配置不生效 spring的基础JDBC配置

    依赖 创建一个 Spring Boot 工程时,可以继承自一个 spring-boot-starter-parent ,也可以不继承 先来看 parent 的基本功能有哪些? 定义了 Java 编译版 ...

  9. 【Spring实战】—— 2 构造注入

    2019独角兽企业重金招聘Python工程师标准>>> 本文讲解了构造注入以及spring的基本使用方式,通过一个杂技演员的例子,讲述了依赖注入属性或者对象的使用方法. 如果想要使用 ...

最新文章

  1. python dict 字典 清空
  2. linux系统各文件夹的作用,linux系统文件夹的作用 good
  3. 并发编程-11线程安全策略之线程封闭
  4. docker 不挂断创建容器
  5. 【Linux系统编程】Linux 信号列表
  6. 高级软件工程第七次作业:东理三剑客团队作业-随笔5
  7. 超18万人次下载使用的 Cloud Toolkit 的成长历程
  8. heroku_如何使用Express.js和Heroku将应用程序部署到Web
  9. SQL update 多表连接方法
  10. mysql加索引后查询时间变长了(终于有头绪了)
  11. 算法设计之数字三角形问题
  12. 为什么要写书?出版图书有哪些好处?
  13. 千万别小看一个面相好的女人!
  14. iVMS-4200 Vs区别_55436红单足球预测 法甲 21:00 安格斯 VS 梅斯
  15. Python 识别录音并转为文字
  16. 2019河南省第十二届ACM大学生程序设计竞赛参赛感
  17. 华为云+AI+5G,点燃2020政企智能升级
  18. esp32使用CH340N实现自动下载电路
  19. android 11.0 12.0自定义开机向导app
  20. 计算机教室安全排查,微机室安全检查制度

热门文章

  1. java面对对象 关键字this super
  2. JS-原型-原型链-值和引用类型
  3. JavaScript 面向对象编程(三) —— 函数进阶 / 严格模式 / 高阶函数 / 闭包 / 浅拷贝和深拷贝
  4. MSP430杂谈--AD7793硬件SPI驱动与模拟SPI驱动
  5. Dex文件格式扫描器:特征API的检测和扫描-小工具一枚(转载)
  6. 哈希表和红黑树的对比
  7. php迭代器实例,PHP迭代器和生成器用法实例分析
  8. php curl密码控件,检索通过curl传递的用户名,密码参数 - php
  9. c++ double 只输出五位_C/C++编程笔记:C语言入门知识点(一),请收藏C语言最全笔记...
  10. veket linux软件下载6,veket linux官方