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 ...
随机推荐
- PHP 输出图像 imagegif 、imagejpeg 与 imagepng 函数
imagegif().imagejpeg().imagepng() 和 imagewbmp() 函数分别允许以 GIF.JPEG.PNG 和 WBMP 格式将图像输出到浏览器或文件. PHP 输出图像 ...
- 面对一个新的MCU,我再也不敢说第一步是点灯了
折腾了几天AT91SAM3S,今天才算是把开发板上的3个LED点亮. 在点亮之前,起码看了百八十页的Datasheet,动用了N次百度. 各种时钟,看门狗,分散加载,中断向量,都得去整.这些都远远超过 ...
- OAF_文件系列3_实现OAF多行表中附件功能AttachmentImage(案例)
20150727 Created By BaoXinjian
- JVM内存结构之二--新生代及新生代里的两个Survivor区(下一轮S0与S1交换角色,如此循环往复)、常见调优参数
一.为什么会有年轻代 我们先来屡屡,为什么需要把堆分代?不分代不能完成他所做的事情么?其实不分代完全可以,分代的唯一理由就是优化GC性能.你先想想,如果没有分代,那我们所有的对象都在一块,GC的时候我 ...
- IOS跳转设置页面及其他各种跳转页面设置
转载来源 CocoaChina 跳到更多设置界面 除了跳到WiFi设置界面,能不能跳到其他的设置界面呢?比如:定位服务.FaceTime.音乐等等.都是可以的,一起来看看如何实现的! 定位服务 定位服 ...
- xml中的非法字符
今使用Jdom生成xml文件的时候,总是出现0x0,0x8为非法字符,经过搜索,问题原因及解决方法如下: 原因:xml中需要过滤的字符分为两类,一类是不允许出现在xml中的字符,这些字符不在xml的定 ...
- ArcGIS 10.1 中的style和serverstyle及制作方法
图层的符号都是随机的,动态变化的,所以不利于图层的美观.还有一点就是符号有增加电子地图可读性的功能. Desktop的符号文件 把要用的符号用desktop的style manage制作相应符号,并把 ...
- Sybase alter 用法
原文地址:http://lujinan858.iteye.com/blog/437019 --示例 1 为表添加列.Adaptive Server 为表中每个现有行分配一个 NULL --列值: al ...
- Python基础篇【第2篇】: Python文件操作
Python文件操作 在Python中一个文件,就是一个操作对象,通过不同属性即可对文件进行各种操作.Python中提供了许多的内置函数和方法能够对文件进行基本操作. Python对文件的操作概括来说 ...
- ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='
多表查询出错,貌似是编码问题. 我比较的两个表的某个字段,设为查询条件,两个字段等于某个值,参照网上的某些论坛调用alter语句,但是依旧没有效果.最后直接拆分成两个条件,a.字段1=x值,b.字段2 ...