在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. Kinetic使用注意点--circle

    new Circle(config) 参数: config:包含所有配置项的对象. { radius: "半径", fill: "填充色", fillRGB: ...

  2. To get TaskID's Integer ID value from the GUID in SharePoint workflow

    list.GetItemByUniqueId(guid).ID int itemID = spList.Items[new Guid("")].ID;

  3. C++内存泄露调试

    我在看DirectX Sample的时候,看到以下代码: // Enable run-time memory check for debug builds. #if defined(DEBUG) | ...

  4. 如何通过logcat查看系统程序的意图

    如果在logcat中不能看到系统程序启动时的意图的类名, 以打开图库(gallery)为例,可以通过在ddms中如图设置,就可以在tomcat中查看到gallery启动时的意图.

  5. uva 10892

    试了一下纯暴力  结果过了 无话可说  应该有更好的方法...... /**************************************************************** ...

  6. windows下游戏服务器端框架Firefly安装说明及demo运行

    原地址:http://blog.csdn.net/wangqiuyun/article/details/11150503 本来公司一个网游服务器端选定了pomelo框架,后来出了个Firefly,为做 ...

  7. c++: 获取delete[]中的数组大小

    看一个小例子: 1 #include <iostream> 2   3 using namespace std; 4   5 class A { 6 public: 7     A() { ...

  8. HDU4607+BFS

    /* bfs+求树的直径 关键:if k<=maxs+1 直接输出k-1: else: k肯定的是包括最长路.先从最长路的起点出发,再走分支,最后到达最长路的终点. 因此是2*(k-(maxs+ ...

  9. thinkphp 定制错误页面

    在前台配置文件里加上: 'TMPL_EXCEPTION_FILE' => '.Public/tpl/error.html',// 异常cuowu页面的模板文件 然后在Public下新建一个tpl ...

  10. SPRING IN ACTION 第4版笔记-第九章Securing web applications-007-设置LDAP server比较密码(contextSource、root()、ldif()、)

    一.LDAP server在哪 By default, Spring Security’s LDAP authentication assumes that the LDAP server is li ...