Spring常用配置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.xsd">

    <!--配置bean
        class:bean 的全类名, 通过反射的方式 在IOC容器中创建Bean, 所以要求Bean中必须有无参数的构造器
        id: 标识容器中的 bean, id 唯一,
    -->
    <bean id ="helloworld" class="com.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property><!--这个是通过setter方法进行最常用的属性注入-->
    </bean>

    <!--通过构造方法来配置bean的属性-->
    <bean id="car" class="com.spring.beans.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="ShangHai" index="1"></constructor-arg>
        <constructor-arg value="300000" index="2"></constructor-arg>
    </bean>

    <!--使用构造器注入属性值可以指定参数的位置和类型!以区分重载的构造器!-->
    <bean id="car2" class="com.spring.beans.Car">
        <constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
        <!--如果字面值包含特殊字符可以使用<![CDATA[]]>包裹起来-->
        <!--属性值也可以直接用<value>标签配置-->
        <constructor-arg type="java.lang.String">
            <value><![CDATA[<ShangHai^>]]></value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>250</value>
        </constructor-arg>
    </bean>

    <bean id="person" class="com.spring.beans.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.spring.beans.Car">
                <constructor-arg value="Ford"></constructor-arg>
                <constructor-arg value="Changan"></constructor-arg>
                <constructor-arg value="200000" type="double"></constructor-arg>
            </bean>
        </property>
        <property name="car.maxSpead" value="260"></property>

    </bean>

    <bean id="person2" class="com.spring.beans.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>
        <!--为级联属性赋值,注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常,和Struts不同-->
        <property name="car.maxSpead" value="250"></property>


    </bean>


    <!--测试如何配置集合属性-->

    <bean id="person3" class="com.spring.beans.collections.Person">
        <property name="name" value="Mike"></property>
        <property name="age" value="27"></property>
        <property name="cars">
            <!--使用list节点为List类型的属性赋值-->
            <list>
                <ref bean="car"/>
                <ref bean="car2"/>
                <bean class="com.spring.beans.Car">
                    <constructor-arg value="Ford"></constructor-arg>
                    <constructor-arg value="Changan"></constructor-arg>
                    <constructor-arg value="200000" type="double"></constructor-arg>
            </bean>
            </list>
        </property>
    </bean>

    <!--配置Map 属性值-->
    <bean id="newPerson" class="com.spring.beans.collections.NewPerson">
        <property name="name" value="Rose"></property>
        <property name="age" value="28"></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.spring.beans.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.spring.beans.collections.Person">
        <property name="name" value="Jack"></property>
        <property name="age" value="29"></property>
        <property name="cars" ref="cars"></property>
    </bean>

    <!--通过p命名空间为bean的属性赋值,需要先导入p命名空间,相对于传统的配置方式更加的简洁-->
    <bean id="person5" class="com.spring.beans.collections.Person" p:age="30"
          p:name="Queen" p:cars-ref="cars"></bean>

    <!--<bean id ="helloworld2" class="com.spring.beans.HelloWorld">
        <property name="name" value="Spring"></property>
    </bean>-->
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值