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 ...
随机推荐
- .Net操作Excel
先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用. .Net导出代码: /// <summ ...
- LVS-DR工作原理图文详解
为了阐述方便,我根据官方原理图另外制作了一幅图,如下图所示:VS/DR的体系结构: 我将结合这幅原理图及具体的实例来讲解一下LVS-DR的原理,包括数据包.数据帧的走向和转换过程. 官方的原理说明:D ...
- 微分方程——基本概念和常微分方程的发展史
1.2 基本概念和常微分方程的发展史 自变量.未知函数均为实值的微分方程称为实值微分方程:未知函数取复值或变量及未知函数均取复值时称为复值微分方程.若无特别声明,以下均指实变量的实值微分方程. 1.2 ...
- (C#) 反转字符串,反转一个句子中单词。
这个是非常基本的一道面试题,但是要考虑周全. 首先反转一个字符串: 基本思路是变成Char数组,然后调用C#里面的方法,或者设定两个index,从头,尾向中间遍历,并交换. 方法一: Array.Re ...
- 用C#开发了四天的UWP应用有感
第一个就是异步方法,async-await,所谓async关键字,并没有什么实际上的语法意义,只是写在函数签名的位置让编译器方便进行查找以及静态检查,并且提醒程序员这是一个异步方法而已.至于await ...
- C#微信json结构接收参数 转载
http://blog.csdn.net/u010773333/article/details/48524155 发素材的时间要上传资源故此要用json格式数据,需要转化. 微信服务器交互基本上都是j ...
- js json 与字符串 转换过程由于书写不统一规范引发的一个问题
对于两个字符串: 字符串1:{title:{},tooltip:{trigger:"axis"},legend:{data:["新关注人数"]},calcula ...
- eclipse 工程默认编码修改 JSP编码修改
1. Window->Preferences->General->Workspace->Text file encoding 将其改为UFT-8 新建的文件即为UTF-8编码 ...
- 8. String to Integer (atoi)
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- ztree
http://www.s u c h s o.com/projecteactual/ztree-jiaocheng-mvc-checkbox-quanxuan-demo-down.html http: ...