Spring的三种注入类型
来源:互联网 发布:人体辉光 知乎 编辑:程序博客网 时间:2024/05/16 23:39
针对注入的值,Spring支持三种类型:字面值、其他Bean的引用和集合类型。
1.字面值
字面值一般指的是可用字符串表示的值,在Spring配置文件中,如果Bean需要注入的值是字面值,则可通过<bean>或者<constructor-arg>的<value>子元素或value属性注入,如:
<?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-2.5.xsd"> <bean id="book" class="com.loster.li.Book"><property name="name" value="SSH开发"></property><property name="author" value="xiaoli"></property><property name="publishHouse" ><value>某出版社</value></property><property name="price"><value>45</value></property></bean></beans>
2.其他Bean的引用
如果Bean需要注入的属性是对象类型,则可以引用IoC容器中定义的类型匹配的其他Bean。引用其他Bean时可以通过<bean>或<constructor-arg>的<ref>子元素或者ref属性注入。
假设在上面的Book类中添加一个属性Item(是Item类的一个对象),而且Item类的属性包含id和count,则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-2.5.xsd"> <bean id="book" class="com.loster.li.Book"><property name="name" value="SSH开发"></property><property name="author" value="xiaoli"></property><property name="publishHouse" ><value>某出版社</value></property><property name="price"><value>45</value></property><property name="Item" ref="Item1"></property><bean id="Item1" class="com.loster.li.Item"><property name="id" value="10"></property><property name="count" value="100"></property></bean></bean></beans>
3.集合类型
Spring还支持对Bean的集合类型属性进行注入,<array>、<list>、<set>、<map>和<prop>元素分别用来设置类型为数组、List、Set、Map和Properties的集合属性值。
0 0
- Spring的三种注入类型
- Spring容器三种注入类型
- Spring笔记之七(Types of Injection) Spring的三种依赖注入实现类型
- Spring框架学习之三(类型数据的注入)
- 依赖注入的三种实现类型
- Spring的几种注入类型
- spring的三种注入方式
- spring依赖注入的三种方式
- Spring的三种注入方式
- Spring中注入的三种方式
- Spring中注入的三种方式
- Spring中注入的三种方式
- Spring的三种注入方式
- spring的三种注入方式:
- Spring的注入方式(三种)
- Spring的三种注入方式?
- Spring依赖注入的三种方式
- Spring依赖注入的三种方式
- 数据结构学习记录1
- 欧拉工程第三题 找出一个合数的最大质数因子
- Android开发艺术探索——第二章:IPC机制(中)
- java空指针问题
- Web前端性能优化的14条规则
- Spring的三种注入类型
- 1008 Java匿名内部类
- C语言基础知识整理(二)
- ListView通过不同列的数据实现某一列颜色变化
- 栈的压入,弹出序列
- leetcode之返回交叉的节点
- 为什么要这样。没有理由
- JavaScript学习笔记(10)
- JSON网络传输协议