java之spring之依赖注入
2024-09-08 10:18:02
一.DI: Dependency injection; 依赖注入
依赖注入和控制反转是同一个概念的不同说法。
对象的创建依赖于容器。对象属性的设置是由容器来设置。
对象属性的赋值过程称为注入。
二.Spring中如何注入属性:
1.普通属性(String 和 基本数据类型),直接通过 property 设置即可
<bean id="user" class="cn.sxt.vo.User">
<property name="name" value="张三疯"/>
<property name="age" value="22"/>
</bean>
2.数组的设置
<property name="hobbies">
<array>
<value>足球</value>
<value>蓝球</value>
<value>乒乓球</value>
</array>
</property>
3.List 的设置和数组一样
<property name="addreess">
<list>
<value>北京昌平</value>
<value>山西平遥</value>
<value>xxxx</value>
</list>
</property>
或者
<property name="addreess">
<array>
<value>北京昌平</value>
<value>山西平遥</value>
<value>xxxx</value>
</array>
</property>
4. set 集合设置
<property name="books">
<set>
<value>大话设计模式</value>
<value>head.first java</value>
</set>
</property>
5.Map集合设置
<property name="cards">
<map>
<entry key="农业银行">
<value>ABC</value>
</entry>
<entry key="工商银行" value="ICBC"/>
</map>
</property>
6. Properties注入
<property name="appearance">
<props>
<prop key="weight">60kg</prop>
<prop key="height">170cm</prop>
</props>
</property>
7. 对象的注入
<!--
对象的注入 ref引用的是 容器中另外一个对象的标识符
-->
<property name="role" ref="myRole"/>
</bean>
<bean id="myRole" class="cn.sxt.vo.Role">
<property name="id" value="1001"/>
<property name="name" value="管理员"/>
</bean>
8. p 命名空间注入
需要导入头文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
配置
<!-- p-property命名空间的注入 本质是属性,需要为属性提供set方法,只是将属性写bean的属性中 -->
<bean id="r1" class="cn.sxt.vo.Role" p:id="1007" p:name="vip"></bean>
9. c命名空间注入
需要导入头文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
配置
<!-- c-constructor命名空间注入 本质是有参构造方法注入,需要提供对应有参构造方法 -->
<bean id="r2" class="cn.sxt.vo.Role" c:id="1006" c:name="普通会员"></bean>
10. Null 注入
<bean id="r3" class="cn.sxt.vo.Role">
<property name="id" value="10"/>
<property name="name"><null/></property>
</bean>
总结:在 spring 中,属性的注入大体上分为两类;
1.构造器注入
2. Set方法注入
需要注意的是:使用构造器注入时,需要提供对应的构造方法;使用 set 方法注入时,需要提供对应的 set 方法。
最新文章
- deep learning...深入学习深度学习 --工具篇
- 准备使用 Office 365 中国版--安装
- document获取节点byId&;byName
- Codeforces Round #379 (Div. 2) 解题报告
- Tomcat配置文件server.xml
- 【Java 基础篇】【第七课】组合
- MySQL中like的使用方法
- 委托delegate使用方法
- Java函数参数传递方式详解
- pc机安装centos6.5,提示sda必须有一个GPT磁盘标签处理
- jersey + tomcat 实现restful风格
- IOS学习--UIButton常用方法(20150122)
- 【ecos学习5】redboot 加载运行hello world
- 走在spring的路上。。。。
- sql server使用sql语句上传Excel到数据库
- fzyzojP1635 -- 平均值
- Linux 典型应用之WebServer 安装和配置
- python学习笔记(11)--数据组织的维度
- qml: 自定义输入框
- application/json和application/x-www-form-urlencoded使用选择
热门文章
- Nginx服务器作反向代理实现内部局域网的url转发配置
- JavaScript初探系列(六)——函数
- 【转】目前为止最透彻的的Netty高性能原理和框架架构解析
- 【转】Android 将自己的应用改为系统应用
- git 下载指定tag版本的源码<;转>;
- SpringBoot——Profile多环境支持
- linux中高并发socket最大连接数的优化详解
- gcc编译链接std::__cxx11::string和std::string的问题
- SQLServer len 函数, 查字符串长度函数
- str.replace替换变量名的字符串