NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
>### 1.出现特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来 ~~~ <bean id="d4" class="cn.li.lesson3.Dog"> <!-- 使用子节点和 <![CDATA[<小黑>]]>给value设置特殊字符--> <constructor-arg index="0"> <value><![CDATA[<小黑>]]></value> </constructor-arg> </bean> ~~~ >### 2.ref 引用其他的bean ~~~ <!-- 此时ref属性的值 就是你要引用得bean的id... --> <property name="pet" ref="dog"></property> <!-- 或者 --> <property name="pet"> <ref bean="dog"></ref> (或者直接写成<ref bean="dog"/>) </property> ~~~ >### 3.内部bean不需要设置任何 id 或 name 属性 ,不能使用在任何其他地方 ~~~ <property name="pet"> <!-- 内部 Bean 声明直接包含在 <property> 或 <constructor-arg> 元素里, 不需要设置任何 id 或 name 属性 --> <bean class="com.igeek.demo3.Pet"> <property name="petName" value="小橘"></property> <property name="petType" value="猫"></property> </bean> </property> ~~~ >### 4.注入null值,使用<null/>标签 ~~~ <constructor-arg name="name"> <null/> </constructor-arg> ~~~ >### 5.null值以及级联属性的注入 ~~~ <bean id="timo" class="com.igeek.demo3.Pet"> </bean> <!-- 添加有参构造器,不要忘记添加无参构造器 --> <bean id="xl" class="com.igeek.demo3.Person"> <constructor-arg name="name"> <null/> </constructor-arg> <constructor-arg name="age" value="20"></constructor-arg> <constructor-arg name="pet" ref="timo"></constructor-arg> <!-- 为级联属性赋值 。前提是上面的pet属性不能为null --> <property name="pet.petName" value="提莫"></property> <property name="pet.petType" value="LOL"></property> </bean> ~~~ >### 6.配置集合类型的属性(List,Array,Set) ~~~ <bean id="dog" class="com.igeek.demo4.Pet"> <property name="petName" value="大黄"></property> <property name="petType" value="犬"></property> </bean> <bean id="cat" class="com.igeek.demo4.Pet"> <property name="petName" value="小橘"></property> <property name="petType" value="猫"></property> </bean> <bean id="tom" class="com.igeek.demo4.Person"> <property name="pets"> <!-- 如果数据类型为数组,也是使用<list>标签进行配置。 如果是Set集合,使用<set>标签进行配置。 (里面内容的配置方式都相同。) --> <list> <ref bean="dog"/> <ref bean="cat"/> <null/> <bean id="snake" class="com.igeek.demo4.Pet"> <property name="petName" value="大象"></property> <property name="petType" value="蛇"></property> </bean> </list> </property> </bean> ~~~ >### 7.配置map类型 ~~~ <bean id="jack" class="com.igeek.demo4.Person"> <property name="map"> <map> <entry key="aa" value-ref="cat"></entry> <entry key="bb" value-ref="dog"></entry> </map> </property> </bean> ~~~ >### 8.配置properties类型 ~~~ <bean id="testpros" class="com.igeek.demo4.Person"> <property name="pros"> <props> <prop key="user">root</prop> <prop key="password">root123</prop> <prop key="jdbcUrl">jdbc:mysql:///test</prop> <prop key="driverClass">com.mysql.jdbc.Driver</prop> </props> </property> </bean> ~~~ >### 9.配置工具类集合 ~~~ <!-- 首先导入util命名空间 --> <?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" 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.2.xsd"> </beans> <util:list id="pets"> <ref bean="cat"/> <ref bean="dog"/> </util:list> <bean id="p4" class="cn.li.lesson5.Person"> <property name="pets"> <ref bean="pets"/> </property> </bean> ~~~ >### 10.使用p命名空间 ~~~ <!-- 首先导入p命名空间 --> xmlns:p="http://www.springframework.org/schema/p" <bean id="p1" class="cn.li.lesson6.Person" p:name="玨网" p:age="20" p:pet-ref="pet"> </bean> <bean id="pet" class="cn.li.lesson6.Pet"> <property name="name" value="鹦鹉"></property> <property name="type" value="鸟"></property> </bean> <!-- 使用p命名空间(spring 2.5版本引入的) 可以简化bean的配置 --> ~~~ >### 11.自动装配 ~~~ autowire = "byName" || "byType" 不推荐使用。 <!-- 可以通过autowire属性设置bean的自动装配 。 byName 根据bean的名字和bean,setter风格的属性名进行自动装配,如有匹配则自动装配,没有不装配。 byType 根据bean的类型,和当前bean属性的类型进行自动装配。如果ioc容器有1个以上类型匹配的bean的时候,则会抛出异常。 constructor 当前bean中有多个构造器的时候 会很复杂不推荐使用该方式。 自动装配的弊端。 1.autowire属性是配置在bean级别的,如果只希望装配个别属性,则不够灵活。 2.要么根据类型 要么根据名字自动装配 不能两个一起使用。 在使用spring整合第三方框架的时候 会使用autowire 。。。 --> <!-- <bean id="person" class="cn.li.lesson6.Person" p:name="tom" p:age="20" autowire="byType"></bean> <bean id="timo" class="cn.li.lesson6.Pet" p:name="猪八戒" p:type="pig"></bean> --> <bean id="p" class="cn.li.lesson6.Person" p:name="tom" p:age="20" autowire="byName"></bean> <bean id="pet" class="cn.li.lesson6.Pet" p:name="猪八戒" p:type="猪"></bean> ~~~