在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的详细知识的更多相关文章

  1. Spring -- 配置bean的三种方法

    配置通过静态工厂方法创建的bean public class StaticBookFactory { //静态工厂方法: public static Book getBook(String bookN ...

  2. spring 配置bean

    Main(测试方法) public class Main { public static void main(String[] args) { //1.创建Spring 的IOC容器对象: //spr ...

  3. spring 配置属性的详细信息

    摘要(这篇文章讲的红,蓝说这话节) 字面值 字面值:可用字符串表示的值,能够通过<value>元素标签或value属性进行注入 基本数据类型及其封装类.String等类型都能够採取字面值注 ...

  4. Spring配置Bean,为属性赋值

    SayHello的实体类: package com.langchao; /** * @ClassName: SayHello * @description: * @author: ZhangYawei ...

  5. Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件

    Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...

  6. Spring(九):Spring配置Bean(二)自动装配的模式、Bean之间的关系

    XML配置里的Bean自动装配 Spring IOC容器可以自动装配Bean,需要做的仅仅是在<bean>的autowire属性里指定自动装配的模式,模式包含:byType,byName, ...

  7. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  8. spring 配置bean的方法及依赖注入发方式

    Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...

  9. Spring入门第二课:Spring配置Bean的细节

    1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...

随机推荐

  1. 自动化运维——一键安装MySQL

    根据项目需要,前段时间在搞EMM系统各种安装包的自动化部署工作,主要包括一键安装和一键启动\停止功能.总结记录下来,以供后用. 本文主要是自动安装MySQL5.7.11版,Linux版脚本在CentO ...

  2. OOP三类继承的区别

    OOP继承的区别提纲: 1. 普通类继承,并非一定要重写父类方法.2. 抽象类继承,如果子类也是一个抽象类,并不要求一定重写父类方法.如果子类不是抽象类,则要求子类一定要实现父类中的抽象方法.3. 接 ...

  3. random note

    今天才慢慢意识到,什么才是学习,(以思考解决问题为驱动),埋头刷分只是方法,不是目的和原动力. 既然准备读研,就要慢慢去了解研究生的生活学习方式是什么样的,涉及到哪些方面. 读研之前要选好方向,但是现 ...

  4. html template

    https://wrapbootstrap.com/tag/single-page http://themeforest.net/ https://wrapbootstrap.com/themes h ...

  5. 3.4 spring- lookup-method 子元素的使用与解析

    1. lookup-method的应用: 1.1 子元素lookup-method 似乎不是很常用,但是在某些时候他的确是非常有用的属性,通常我们称它为 "获取器注入" . 引用 ...

  6. vncserver 添加用户

    1.在vncserver设置登录用户的信息   #vi /etc/sysconfig/vncservers   VNCSERVERS="1:root 2:wt" 此处添加用户   ...

  7. PAT-乙级-1010. 一元多项式求导 (25)

    1010. 一元多项式求导 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 设计函数求一元多项式的导数.(注:xn(n为整数)的一 ...

  8. 算法导论_ch2

    Ch2算法基础 whowhoha@outlook.com 2.1 插入排序 输入:n个数的一个序列〈a1,a2,…,an〉. 输出:输入序列的一个排列〈a′1,a′2,…,a′n〉,满足a′1≤a′2 ...

  9. fiddler 插件开发

    本文主要讲解使用.net C#语言开发Fiddler插件. 1.在Fiddler 会话列表中添加自定义列 使用FiddlerApplication.UI.lvSessions.AddBoundColu ...

  10. STL,ATL,WTL之间的联系和区别

    STL即 Standard Template Library (标准模板库) STL是惠普实验室开发的一系列软件的统称.它是由Alexander Stepanov.Meng Lee和David R M ...