Spring中bean的设置

来源:互联网 发布:网络频段是什么意思 编辑:程序博客网 时间:2024/05/21 17:02

Spring中bean的设置


  1 <?xml version="1.0" encoding="UTF-8"?>

  2 <beans xmlns="http://www.springframework.org/schema/beans"  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  4     xmlns:util="http://www.springframework.org/schema/util"  5     xmlns:p="http://www.springframework.org/schema/p"  6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  7         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

  3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4     xmlns:util="http://www.springframework.org/schema/util"

  5     xmlns:p="http://www.springframework.org/schema/p"

  6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

  7         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">

  8 

   <!-- 配置bean  10         class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器 11         id:标识容器中的bean,id唯一。 12     -->
 10         class:bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器

  11id:标识容器中的bean,id唯一。

 12     -->

 13     <bean id="helloSpring" class="com.yl.HelloSpring">
 14         <property name="name" value="Spring"></property>
 15     </bean>

 16     <!-- 使用构造器注入属性值可以指定参数的位置和参数的类型,以区分重载的构造器 -->

 17     <bean id="car" class="com.yl.Car">
 18         <constructor-arg value="Audi" index="0"></constructor-arg>
 19         <constructor-arg value="Shanghai" index="1"></constructor-arg>
 20         <constructor-arg value="300000" type="double"></constructor-arg>
 21     </bean>

 22     

 23     <bean id="car2" class="com.yl.Car">
 24         <constructor-arg value="BMW" type="java.lang.String"></constructor-arg>
 25         <!-- 如果字面值包含特殊字符,可以使用<![CDATA[]]> 包裹起来-->
 26         <!-- 属性值还可以使用value子节点进行配置 -->
 27         <constructor-arg type="java.lang.String">
 28             <value><![CDATA[<ShangHai~>]]></value>
 29         </constructor-arg>
 30         <constructor-arg value="200" type="int"></constructor-arg>
 31     </bean>  
 34     <bean id="person" class="com.yl.Person">
 35         <property name="name" value="Tom"></property>
 36         <property name="age" value="24"></property>
 37         <!-- 可以使用property的ref属性建立bean之间的引用关系 -->
 38         <!-- <property name="car" ref="car2"></property> -->
 39         <!-- <property name="car"> 40             <ref bean="car2"/> 41         </property> -->
 40             <ref bean="car2"/>
 41         </property> -->

 42         <!-- 内部Bean,不能被外部引用,只能在内部使用  -->

 43         <property name="car">
 44             <bean class="com.yl.Car">
 45                 <constructor-arg value="Ford"></constructor-arg>
 46                 <constructor-arg value="BeiJing"></constructor-arg>
 47                 <constructor-arg value="100000"></constructor-arg>
 48             </bean>
 49         </property>
 50     </bean>     

 53     <bean id="person2" class="com.yl.Person">
         <constructor-arg value="Jerry"></constructor-arg>
 55         <constructor-arg value="25"></constructor-arg>
 56         <!-- <constructor-arg ref="car"></constructor-arg> -->
 57         <!-- 测试null值 -->
 58         <!-- <constructor-arg><null/></constructor-arg> -->
 59         <!-- 为级联属性赋值。注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常 -->
 60         <constructor-arg ref="car"></constructor-arg>
 61         <property name="car.speed" value="260"></property>

 62     </bean>

 63     

 64     

 65     <!-- 测试集合属性 -->

 66     <bean id="person3" class="com.yl.collections.Person">
 67         <property name="name" value="Mike"></property>
 68         <property name="age" value="30"></property>
 69         <property name="cars">
 70             <!-- 使用list节点为List类型的属性赋值 -->

 71             <list>
 72                 <ref bean="car"/>
 73                 <ref bean="car2"/>
 74                 <bean class="com.yl.Car">
 75                     <constructor-arg value="Ford"></constructor-arg>
 76                     <constructor-arg value="BeiJing"></constructor-arg>
 77                     <constructor-arg value="100000"></constructor-arg>
 78                 </bean>
 79             </list>
 80         </property>

 81     </bean>    

 83     <!-- 配置Map属性值 -->

 84     <bean id="newPerson" class="com.yl.collections.NewPerson">
 85         <property name="name" value="Rose"></property>
 86         <property name="age" value="24"></property>
 87         <property name="cars">
 88             <!-- 使用map节点机map的entry子节点配置Map类型的成员变量 -->
 89             <map>
 90                 <entry key="AA" value-ref="car"></entry>
                <entry key="BB" value-ref="car2"></entry>
 92             </map>
 93         </property>
 94     </bean>     

 96     <!-- 配置Properties属性值 -->

 97     <bean id="dataSource" class="com.yl.collections.DataSource">
 98         <property name="properties">
 99             <!-- 使用props和prop子节点来为Properties属性赋值 -->
100             <props>
101                 <prop key="user">root</prop>
102                 <prop key="password">1234</prop>
103                 <prop key="jdbcUrl">jdbc:mysql:///test</prop>
104                 <prop key="driverClass">com.mysql.jdbc.Driver</prop>
105             </props>
106         </property>
107     </bean>
 </beans>


0 0
原创粉丝点击