spring bean xml配置

spring bean配置

<?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.xsd
">
<!--  <import resource="beans2.xml"></import>-->
<!--  为bean起别名-->
  <alias name="user3" alias="toName"/>
<!--普通bean 属性int 和 string 试用get和set方法赋值-->
  <bean id="user" class="com.example.springdemo.pojo.User">
  <property name="id" value="88"></property>
    <property name="uname" value="caidachun"></property>
  </bean>
  <!--普通bean 属性int 和 string 使用构造方法赋值-->
  <bean id="user3" class="com.example.springdemo.pojo.User" name="kang3">
    <constructor-arg name="id" value="3" ></constructor-arg>
    <constructor-arg name="uname" value="kang"></constructor-arg>
  </bean>
<!--  属性是引用类型 使用ref赋值-->
  <bean id="person" class="com.example.springdemo.pojo.Person">
    <property name="user" ref="kang3"></property>
  </bean>
<!-- 属性是引用类型 使用ref标签嵌套赋值 -->
  <bean id="person1" class="com.example.springdemo.pojo.Person">
    <property name="user">
      <ref bean="user"></ref>
    </property>
  </bean>
<!--  使用工厂bean装配bean 指定工厂类和产生bean的方法-->
  <bean id="getfactoryuser" factory-method="getuser" class="com.example.springdemo.pojo.factory"></bean>
<!--  使用p命名标签简化,不用书写property标签,直接在bean标签中书写-->
  <bean id="puser" class="com.example.springdemo.pojo.User" p:id="12" p:uname="hehe">
  </bean>
<!--  使用null标签赋予空值,还有bean中的特殊符号尖括号
  1 把<>进行转义 &lt; &gt;
  2 把带特殊符号内容写到 CDATA
-->
  <bean id="person-null" class="com.example.springdemo.pojo.Person">
    <property name="user"><null></null></property>
  </bean>
<!--内部bean不用指定id和name-->
  <bean id="person-neibu" class="com.example.springdemo.pojo.Person">
    <property name="user">
      <bean class="com.example.springdemo.pojo.User">
        <property name="id" value="33"></property>
        <property name="uname" value="neibu"></property>
      </bean>
    </property>
  </bean>
<!--  这种方法多次一举-->
  <bean name="jilianperson" class="com.example.springdemo.pojo.Person">
    <property name="user" ref="jilian"></property>
    <property name="user.id" value="55"></property>
    <property name="user.uname" value="jilianbean55"></property>
  </bean>
  <bean name="jilian" class="com.example.springdemo.pojo.User"></bean>
  <bean name="testlist" class="com.example.springdemo.pojo.ListTest">
    <property name="list">
      <array>
        <value>list-1</value>
        <value>list-2</value>
        <value>list-3</value>
      </array>

    </property>
    <property name="set">
      <set>
        <value>set1</value>
        <value>set2</value>
        <value>set2</value>
      </set>
    </property>
    <property name="map">
      <map>
        <entry key="1" >
          <value>map1</value>
        </entry>
        <entry key="2">
          <value>map2</value>
        </entry>
        <entry key="3">
          <value>map3</value>
        </entry>
      </map>
    </property>
  </bean>
<!--  引入util命名空间 编写外部list,map,set,里面的内容可以是普通类型也可以是引用类型-->
  <util:list id="listbean1">
    <array>
      <ref bean="kang3"></ref>
      <ref bean="kang3"></ref>
    </array>
  </util:list>
  <util:map id="mapref">
    <entry  key-ref="kang3" value-ref="kang3">
    </entry>
    <entry key="123" value="321"></entry>
  </util:map>
  <util:set id="setref">
    <ref bean="kang3"></ref>
  </util:set>
  <bean id="listref" class="com.example.springdemo.pojo.ListTest">
    <property name="map">
      <ref bean="mapref"></ref>
    </property>
    <property name="set">
      <ref bean="setref"></ref>
    </property>
    <property name="list">
      <ref bean="listbean1"></ref>
    </property>
  </bean>
</beans>在这里插入代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值