1). 若字面值中包括特殊字符,则能够使用
value 节点的 <![CDATA[]]> 把字面值包裹起来。



     <constructor-arg>

          <!-- 若 value 属性值中包括特殊字符串, 则能够使用 value 子节点来注入属性值. value 子节点中能够使用 CDATA -->

          <value><![CDATA[Zheng <><> zhou]]></value>

     </constructor-arg>



2). 在 Bean 的配置文件里, 能够通过 <ref> 元素或 ref  属性为
Bean 的属性或构造器參数指定对 Bean 的引用.



     <bean id="dao" class="com.atguigu.spring.ioc.ref.Dao">

          <property name="database" value="DB2"></property>

     </bean>



     <bean id="service2" class="com.atguigu.spring.ioc.ref.Service">

          <property name="dao" ref="dao"></property>

     </bean>

     ------------------------------------------------------------------------------

     解析:<property name="dao" ref="dao"></property> 的作用为:

          Dao dao = (Dao)ctx.getBean("dao");

          Service service = (Service)ctx.getBean("service2");

          service.setDao(dao);



3). 能够在属性或构造器里包括 Bean 的声明, 这种 Bean 称为内部 Bean



     <bean class="com.atguigu.spring.ioc.ref.Service" id="service">

          <property name="dao">

               <bean class="com.atguigu.spring.ioc.ref.Dao">

                    <property name="database" value="MySQL"></property>

               </bean>

          </property>

     </bean>



     解释:类似于下面代码,但 dao 的这个 bean 事实上是没有 id 的,也不能被其它的 bean 来引用。也不能单独从 IOC 容器中获取。



          <bean class="com.atguigu.spring.ioc.ref.Dao" id="dao">

               <property name="database" value="MySQL"></property>

          </bean>



          <bean class="com.atguigu.spring.ioc.ref.Service" id="service">

               <property name="dao" ref="dao"></property>

          </bean>         



          ①. 当 Bean 实例只给一个特定的属性使用时, 能够将其声明为内部 Bean.

                内部 Bean 声明直接包括在 <property> 或 <constructor-arg> 元素里, 不须要设置不论什么 id 或 name 属性

          ②. 内部 Bean 不能使用在不论什么其它地方



4). 能够使用专用的 <null/> 元素标签为 Bean 的字符串或其他对象类型的属性注入
null 值



     <bean id="car6" class="com.atguigu.spring.ioc.Car">

          <!-- 为 maxSpeed 赋值为 null, 而 value="null" 是把 null 这个字符串赋给了相应的属性 -->

          <property name="corp"><null/></property>

     </bean>





5). Spring 支持级联属性的配置。



     <bean id="action3" class="com.atguigu.spring.ioc.ref.Action">

          <property name="service" ref="service2"></property>



          <!-- 为 service 的 dao 的 database 赋值为 ORACLE -->

          <property name="service.dao.database" value="ORACLE"></property>

     </bean>



6). 配置 java.util.List 类型的属性, 须要指定 <list>  标签, 在标签里包括一些元素.

     这些标签能够通过 <value> 指定简单的常量值, 通过 <ref> 指定对其它 Bean 的引用.

     通过<bean> 指定内置 Bean 定义.

     通过 <null/> 指定空元素. 甚至能够内嵌其它集合.



     <property name="cars">

          <!-- 通过 list 指定集合属性的值. 但 list 是一个内部 list, 不能被其它的 bean 引用.  -->

          <list>

               <!-- ref 直接指向已有的 bean -->

               <ref bean="car"/>

               <ref bean="car2"/>

               <ref bean="car3"/>

               <!-- bean 声明内部 bean -->

               <bean class="com.atguigu.spring.ioc.Car">

                    <property name="brand" value="BMW"></property>

                    <property name="corp" value="HuaChen"></property>

                    <property name="maxSpeed" value="300"></property>

                    <property name="price" value="800000"></property>

               </bean>

          </list>

     </property>



7).  能够使用 util schema 里的集合标签定义独立的集合 Bean. 须要注意的是, 必须在 <beans> 根元素里加入 util schema 定义



①. 导入 util 命名空间



<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.0.xsd">





②. 定义集合 Bean



     <util:list id="cars">

          <!-- ref 直接指向已有的 bean -->

          <ref bean="car"/>

          <ref bean="car2"/>

          <ref bean="car3"/>


          <!-- bean 声明内部 bean -->

          <bean class="com.atguigu.spring.ioc.Car">

               <property name="brand" value="BMW"></property>

               <property name="corp" value="HuaChen"></property>

               <property name="maxSpeed" value="300"></property>

               <property name="price" value="800000"></property>

          </bean>

     </util:list>



8). 配置 Map Bean



     <util:map id="config">

          <entry key="user" value="root"></entry>

          <entry key="password" value="1230"></entry>

          <entry key="driverClass" value="com.mysql.jdbc.Driver"></entry>

          <entry key="jdbcUrl" value="jdbc:mysql:///test"></entry>

     </util:map>



9). 配置 Properties 的属性



     <property name="properties">

          <props>

               <prop key="hibernate.hbm2ddl.auto">update</prop>

               <prop key="b">B</prop>

               <prop key="c">C</prop>

               <prop key="d">D</prop>

          </props>

     </property>



10). 使用 p 命名空间简化 bean 的属性配置:



     ①. 导入  p 的命名空间



     <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.0.xsd">





     ②. 使用 p 命名空间进行配置



     <bean id="car7" class="com.atguigu.spring.ioc.Car"

               p:brand="Mazda"

               p:corp="ChangAn"

               p:maxSpeed="220"

               p:price="200000"/>

3、Spring4之Bean 配置的细节的更多相关文章

  1. 2 Spring4 之Bean的配置

    Spring4 之Bean的配置 1 IOC & DI 概述 IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源 ...

  2. [原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. 丢弃重口味的xml配置--spring4用groovy配置bean(转)

    spring4之前,bean的配置可以主要分为两种方式,一种是使用基于xml,个人非常讨厌这种方式,因为明明一件很简单的事,放在xml中就会多了不少繁杂的信息.另一种方式,是从spring3.0开始, ...

  4. Spring4学习笔记二:Bean配置与注入相关

    一:Bean的配置形式 基于XML配置:在src目录下创建 applicationContext.xml  文件,在其中进行配置. 基于注解配置:在创建bean类时,通过注解来注入内容.(这个不好,因 ...

  5. Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...

  6. 关于spring中bean配置的几件小事

    一.IOC和DI 1.IOC(Inversion of Control) 其思想是反转资源获取的方向.传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源:而应用了IOC之后 ...

  7. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. Spring Bean配置2

    Spring表达式语言:SpEL •Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框 ...

  9. Spring Bean配置

    Spring 是什么 •Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. •Spring 是一个 IOC(DI) 和 ...

随机推荐

  1. 怎么获取Spring的ApplicationContext

    在 WEB 开发中,可能会非常少须要显示的获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean, 今天我就遇到了,在这里和大家分享一下, WEB 开发中,怎么获 ...

  2. SICP 习题 (2.6) 解题总结:丘奇计数

    SICP 习题 2.6 讲的是丘奇计数,是习题2.4 和 2.5的延续. 这里大师们想提醒我们思考的是"数"究竟是什么,在计算机系统里能够怎样实现"数".准备好 ...

  3. VMware中linux与window目录共享

    在虚拟机下来实如今windows下共享一个目录: (前提已安装完毕vmtools:http://blog.csdn.net/pipisorry/article/details/21318931) 打开 ...

  4. hdu4105  Electric wave

    Electric wave Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. [华为机试练习题]50.求M的N次方的最后三位

    题目 描写叙述: 正整数M 的N次方有可能是一个很大的数字,我们仅仅求该数字的最后三位 例1: 比方输入5和3 ,5的3次方为125.则输出为125 例2: 比方输入2和10 2的10次方为1024 ...

  6. 可重入锁(good)

    可重入锁,也叫做递归锁,是指在一个线程中可以多次获取同一把锁,比如:一个线程在执行一个带锁的方法,该方法中又调用了另一个需要相同锁的方法,则该线程可以直接执行调用的方法[即可重入],而无需重新获得锁: ...

  7. SVNKIT操作SVN版本库的完整例子

    Model: package com.wjy.model; public class RepositoryInfo { public static String storeUrl="http ...

  8. Java对象序列化/反序列化的注意事项

    Java对象序列化 对于一个存在Java虚拟机中的对象来说,其内部的状态只是保存在内存中.JVM退出之后,内存资源也就被释放,Java对象的内部状态也就丢失了.而在很多情况下,对象内部状态是需要被持久 ...

  9. HDU 4454 - Stealing a Cake(三分)

    我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次 后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值 以下是代码 #i ...

  10. 怎样从host之外连接到docker container

    启动docker的时候的指令使用 sudo docker -H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock -d & 这样就能使dock ...