Spring配置bean的详细知识
在Spring中配置bean的一些细节。具体信息请参考下面的代码及注释
applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!-- 配置bean
class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器
id:标识容器中的bean,id唯一。
-->
<bean id="helloSpring" class="com.yl.HelloSpring">
<property name="name" value="Spring"></property>
</bean>
<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型,以区分重载的构造器 -->
<bean id="car" class="com.yl.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" type="double"></constructor-arg>
</bean> <bean id="car2" class="com.yl.Car">
<constructor-arg value="BMW" type="java.lang.String"></constructor-arg>
<!-- 如果字面值包含特殊字符,可以使用<![CDATA[]]> 包裹起来-->
<!-- 属性值还可以使用value子节点进行配置 -->
<constructor-arg type="java.lang.String">
<value><![CDATA[<ShangHai~>]]></value>
</constructor-arg>
<constructor-arg value="200" type="int"></constructor-arg>
</bean> <bean id="person" class="com.yl.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<!-- 可以使用property的ref属性建立bean之间的引用关系 -->
<!-- <property name="car" ref="car2"></property> -->
<!-- <property name="car">
<ref bean="car2"/>
</property> -->
<!-- 内部Bean,不能被外部引用,只能在内部使用 -->
<property name="car">
<bean class="com.yl.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="BeiJing"></constructor-arg>
<constructor-arg value="100000"></constructor-arg>
</bean>
</property>
</bean> <bean id="person2" class="com.yl.Person">
<constructor-arg value="Jerry"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<!-- <constructor-arg ref="car"></constructor-arg> -->
<!-- 测试null值 -->
<!-- <constructor-arg><null/></constructor-arg> -->
<!-- 为级联属性赋值。注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常 -->
<constructor-arg ref="car"></constructor-arg>
<property name="car.speed" value="260"></property>
</bean> <!-- 测试集合属性 -->
<bean id="person3" class="com.yl.collections.Person">
<property name="name" value="Mike"></property>
<property name="age" value="30"></property>
<property name="cars">
<!-- 使用list节点为List类型的属性赋值 -->
<list>
<ref bean="car"/>
<ref bean="car2"/>
<bean class="com.yl.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="BeiJing"></constructor-arg>
<constructor-arg value="100000"></constructor-arg>
</bean>
</list>
</property>
</bean> <!-- 配置Map属性值 -->
<bean id="newPerson" class="com.yl.collections.NewPerson">
<property name="name" value="Rose"></property>
<property name="age" value="24"></property>
<property name="cars">
<!-- 使用map节点机map的entry子节点配置Map类型的成员变量 -->
<map>
<entry key="AA" value-ref="car"></entry>
<entry key="BB" value-ref="car2"></entry>
</map>
</property>
</bean> <!-- 配置Properties属性值 -->
<bean id="dataSource" class="com.yl.collections.DataSource">
<property name="properties">
<!-- 使用props和prop子节点来为Properties属性赋值 -->
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean> <!-- 配置单例的集合bean,以供多个bean进行引用.需要导入util命名空间 -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list> <bean id="person4" class="com.yl.collections.Person">
<property name="name" value="Jack"></property>
<property name="age" value="25"></property>
<property name="cars" ref="cars"></property>
</bean> <!-- 通过p命名空间为bean的属性赋值,需要先导入p命名空间,相对于传统的方式,更加简洁 -->
<bean id="person5" class="com.yl.collections.Person" p:name="Queen" p:age="27" p:cars-ref="cars"></bean> </beans>
Spring自动装配bean
Spring IOC容器可以自动装配bean,需要做的仅仅是在<bean>的autowire属性里指定装配的模式
byType:根据类型自动装配,若IOC容器中有多个与目标类型一致的bean,在这种情况下,Spring将无法判定哪个bean最合适该属性,所以不能执行
byName:根据名称自动装配,必须将目标bean的名称和属性名设置的完全相同
constructor:通过构造器自动装配,当bean中存在多个构造器时,此种自动装配方式将会很复杂。不推荐使用。具体信息参考下面的文件及注释
beans.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <bean id="address" class="com.yl.autowire.Address"
p:city="BeiJing" p:street="HuiLongGuan"></bean> <bean id="car" class="com.yl.autowire.Car"
p:brand="Audi" p:price="30000"></bean> <!-- <bean id="person" class="com.yl.autowire.Person"
p:name="Tom" p:address-ref="address" p:car-ref="car"></bean> -->
<!-- 可以使用autowire属性指定自动装配的方式,
byName根据bean的名字和当前bean的setter风格的属性名进行自动装配 ,若有匹配的,则进行自动装配,若没有匹配的,则不装配
byType根据bean的类型和当前bean的属性的类型进行自动装配,若IOC容器中有一个以上的类型匹配的bean,则抛出异常
-->
<bean id="person" class="com.yl.autowire.Person"
p:name="Tom" autowire="byName"></bean>
</beans>
自动装配的缺点
在bean配置文件里设置autowire属性进行自动装配将会装配bean的所有属性,然而,若只希望装配个别属性时,autowire就不够灵活了。
autowire属性要么根据类型自动装配,要么根据名称自动装配,不能两者兼而有之
一般情况下,在实际的项目中很少使用自动装配功能,因为和自动装配功能所带来的好处比起来,明确清晰的配置文档更有说服力。
Spring配置bean的详细知识的更多相关文章
- Spring -- 配置bean的三种方法
配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...
- spring 配置bean
Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spr ...
- spring 配置属性的详细信息
摘要(这篇文章讲的红,蓝说这话节) 字面值 字面值:可用字符串表示的值,能够通过<value>元素标签或value属性进行注入 基本数据类型及其封装类.String等类型都能够採取字面值注 ...
- Spring配置Bean,为属性赋值
SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
- Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系
XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...
- Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节
在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...
- spring 配置bean的方法及依赖注入发方式
Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...
- Spring入门第二课:Spring配置Bean的细节
1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...
随机推荐
- CSS 将按钮转成超链接样式
一.将按钮转成超链接样式 .GoStyle { color: #0c5fc4; background-color: #FFFFFF; ...
- ios 基于CAEmitterLayer的雪花,烟花,火焰,爱心等效果demo(转)
转载自:http://blog.csdn.net/mad2man/article/details/16898369 分类: cocoa SDK2013-11-23 11:52 388人阅读 评论(0) ...
- Web Api 控制器
Web Api 控制器 文档目录 本节内容: 简介 AbpApiController 基类 本地化 其它 过滤 审计日志 授权 防伪造过滤 工作单元 结果包装和异常处理 结果缓存 验证 模块绑定器 简 ...
- NOI2015考试小结
这次NOI2015有幸获得金牌考进了国家集训队,意味着我的OI退役时间既省选之后有延迟了好几个月,又有了新的目标吧. 先说一下考试之外的感受吧,学军宿舍很牛X,接待NOIers而不提供插座,唯一可以用 ...
- [百度]数组A中任意两个相邻元素大小相差1,在其中查找某个数
一.问题来源及描述 今天看了July的微博,发现了七月问题,有这个题,挺有意思的. 数组A中任意两个相邻元素大小相差1,现给定这样的数组A和目标整数t,找出t在数组A中的位置.如数组:[1,2,3,4 ...
- [转载].Net中如何操作IIS(源代码)
///***********************************************************///************** IIS控制管理类 1.0 Beta ** ...
- PAT-乙级-1041. 考试座位号(15)
1041. 考试座位号(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 每个PAT考生在参加考试时都会被分 ...
- [BEC][hujiang] Lesson04 Unit1:Working life ---Reading + Listening &Grammar & Speaking
4 1.1 Working life P10 Reading----The anonymous CV Exercise 3 What should be included in the CV ...
- 关于.net中的脚本语言使用
基于.net中drl框架的脚本现在有很多,最近也由于工作的需要,目前有lua.python.ruby.javascript的.net实现,对ruby不怎么了解,python.lua.js就成了试验的对 ...
- Pig安装及简单使用(pig版本0.13.0,Hadoop版本2.5.0)
原文地址:http://www.linuxidc.com/Linux/2014-03/99055.htm 我们用MapReduce进行数据分析.当业务比较复杂的时候,使用MapReduce将会是一个很 ...