Spring IOC容器中注入bean
一、基于schema格式的注入
1、基本的注入方式 (属性注入方式)
根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求
<bean id="student" class="com.lq.ioc.Student">
<property name="name" value="zhansan"></property>
<property name="age" value="20"></property>
</bean>
<bean id="student" class="com.lq.ioc.Student">
<property name="name">
<value>zhangsan</value>
</property>
<property name="age">
<value>20</value>
</property>
</bean>
2.构造函数方式注入
<bean id="student1" class="com.lq.ioc.Student">
<constructor-arg name="name" value="zhangsan"></constructor-arg>
<constructor-arg name="age" value="20"></constructor-arg>
</bean>
<bean id="student1" class="com.lq.ioc.Student">
<constructor-arg type="java.lang.String"><value>lisi</value></constructor-arg>
<constructor-arg type="int"><value>15</value></constructor-arg>
</bean>
<bean id="student1" class="com.lq.ioc.Student">
<constructor-arg index="0" value="zhangsan"></constructor-arg>
<constructor-arg index="1" value="21"></constructor-arg>
</bean>
3.当注入的属性中含有xml中的特殊字符时,如: < > & " '
1.用<![CDATA[<wangwu>]]>
<bean id="student1" class="com.lq.ioc.Student">
<constructor-arg index="0">
<value><![CDATA[<wangwu>]]></value>
</constructor-arg>
<constructor-arg index="1" value="21"></constructor-arg> </bean>
2.用转义字符

<bean id="student1" class="com.lq.ioc.Student">
<property name="name">
<value><wang></value>
</property>
<property name="age" value="11"></property>
</bean>
4.引用其它bean
<bean id="school" class="com.lq.ioc.School"/>
<bean id="student1" class="com.lq.ioc.Student">
<property name="name">
<value><wang></value>
</property>
<property name="age" value="11"></property>
<property name="school" ref="school"></property> </bean>
5.内部bean
<bean id="student1" class="com.lq.ioc.Student">
<property name="name">
<value><wang></value>
</property>
<property name="age" value="11"></property>
<property name="school">
<bean id="school" class="com.lq.ioc.School"/>
</property>
</bean>
6.往bean中注入null
<property name="name">
<null/>
</property>
7.集合类属性
List
<bean id="school" class="com.lq.ioc.School">
<property name="name" value="ql"/>
<property name="student">
<list>
<ref bean="student1"/>
</list>
</property>
</bean>
set
<bean id="school" class="com.lq.ioc.School">
<property name="name" value="ql"/>
<property name="student">
<set>
<ref bean="student1"/>
</set>
</property>
</bean>
map
<bean id="school" class="com.lq.ioc.School">
<property name="name" value="ql"/>
<property name="student">
<map>
<entry>
<key><ref bean="student1"/></key>
<value>1</value>
</entry> </map>
</property>
</bean>
props
<bean id="school" class="com.lq.ioc.School">
<property name="name" value="ql"/>
<property name="student">
<props>
<prop key="student1">zhansa</prop>
<prop key="student2">lisi</prop>
</props>
</property>
</bean>
8.通过util配置集合
<util:properties id="List1">
<prop key="student1">zzz</prop>
<prop key="student2">lisi</prop>
</util:properties>
9.工厂类注入
<bean id="studentFactory" class="com.lq.ioc.StudentFactory"/>
<bean id="student" factory-bean="studentFactory"
factory-method="createStudent"/>
静态工厂类注入
<bean id="student" class="com.lq.ioc.StudentFactory"
factory-method="createStudent"/>
10.配置数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost/sampledb"
p:username="root"
p:password="123"/>
Spring IOC容器中注入bean的更多相关文章
- Spring学习--实现 FactoryBean 接口在 Spring IOC 容器中配置 Bean
Spring 中有两种类型的 bean , 一种是普通的 bean , 另一种是工厂 bean , 即 FactroyBean. 工厂 bean 跟普通 bean 不同 , 其返回的对象不是指定类的一 ...
- spring在IoC容器中装配Bean详解
1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...
- spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
spring 在容器中一个bean依赖另一个bean 需要通过ref方式注入进去 通过构造器 或property
- Spring IOC容器分析(4) -- bean创建获取完整流程
上节探讨了Spring IOC容器中getBean方法,下面我们将自行编写测试用例,深入跟踪分析bean对象创建过程. 测试环境创建 测试示例代码如下: package org.springframe ...
- spring给容器中注入组件的几种方式
目录 环境搭建 spring给容器中注入组件 1.包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)适用于把自己写的类加入组件(默认ID类名 ...
- 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
[spring以及第三方jar的案例]在spring中的aop相关配置的标签,线程池相关配置的标签,都是基于该种方式实现的.包括dubbo的配置标签都是基于该方式实现的.[一]原理 ===>sp ...
- Spring扩展:替换IOC容器中的Bean组件 -- @Replace注解
1.背景: 工作中是否有这样的场景?一个软件系统会同时有多个不同版本部署,比如我现在做的IM系统,同时又作为公司的技术输出给其他银行,不同的银行有自己的业务实现(比如登陆验证.用户信息查询等) ...
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
随机推荐
- 【转】STM32定时器输出比较模式中的疑惑
OCx与OCxREF和CCxP之间的关系 初学STM32,我这个地方卡了很久,现在终于有些明白了,现在把我的理解写下与大家共享,如果有不对的地方,还请指出. OCxREF就是一个参考信号,并且约定: ...
- 学习SQL的点点滴滴(一)-常用函数
该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很详细. 以下所有例子均Studnet表为例 ...
- ios 开发小技巧一
对于UITableViewCell中的textField/textView,你肯定想让它编辑时可以把所在行滚动到键盘上方.如果你的VC是UITableViewController或者子类,那么只要在o ...
- ADF_Controller系列2_绑定TasksFlow、Region和Routers(Part2)
2015-02-14 Created By BaoXinjian
- iOS 手写输入法奔溃,替换隐藏键盘方法
{ UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:se ...
- win10激活
听了同事忽悠,说x230这款还在保,可以直接装win10,自动激活,结果悲剧.送到联想工作站需要摧毁所有数据,然后还要2.3个小时,遂在网上找了个方法,先用着,只是不知道什么时候又变成黑户: 以管理员 ...
- winform下自绘提示框风格窗体
昨天分享了一个环形滚动条控件,今天分享一个提示框风格的窗体.代码如下: /// <summary> /// 继承自Form,但将FormBorderStyle设置为None /// < ...
- uva 1368 DNA Consensus String
这道题挺简单的,刚开始理解错误,以为是从已有的字符串里面求最短的距离,后面才发现是求一个到所有字符串最小距离的字符串,因为这样的字符串可能有多个,所以最后取最小字典序的字符串. 我的思路就是求每一列每 ...
- linux 搭建 nexus 私服及配置
安装篇 1.tar -zxvf nexus-latest-bundle.tar.gz 2.cd nexus-2.13.0-01/bin 3../nexus start 这时可能提示 ********* ...
- C语言的time.h解释python的time
clock_t clock(void),对比python的clock() 返回程序执行的时间,clock_t的实际类型是long #include<Windows.h> Sleep(); ...