Spring學習筆記一

IOC容器裏配置bean
applicationContext.xml
必須有個無參的構造器
class:bean的全類名
經過bean id得到這個bean必須惟一java


spring提供2種IOC容器容器實現方式
beanfactory用在spring自己
applicationContext用在開發者
配置方式相同mysql



ApplicationContext有2個主要的實現類
ClassPathXmlApplicationContext
FileSystemXmlApplicationContextweb


ApplicationContext下面有
ConfigurableApplicationContext啓動更新關閉上下文過程spring



ApplicationContext初始化上下文的時候,實例化全部單例的beansql


WebApplicationContext用於web編程


ApplicationContext的getBean方法讀取beanapp


用類型獲取,容器內只能有一個
ctx.getBean(HelloWorld.class);spa


用id獲取
ctx.getBean(helloworld);code


注入方式
屬性注入(最經常使用)
構造器注入
工廠方法注入xml


//編程
區分重載構造器



字面值可用字符串表示的值,經過value標籤或者value屬性注入,value子節點
//字面值表示特殊字符
<![CDATA[<beijing>]]>


//編程, property 的ref
bean之間的引用


//編程
內部bean不能被外部bean引用


//編程
null值和級聯屬性
<null/>
默認值就是null


//編程
級聯屬性
屬性先初始化後才能賦值,不然異常



//編程List屬性賦值
集合屬性List,也能定義內部bean


//編程
集合屬性Map,Map有k/v


//編程
Properties
HashTable子類
<props>
<prop>


//編程
集合配置
把集合的配置拿出來作成一個公用的bean
namespace: util.list


//編程
使用p命名空間
namespace: p
加入xmlns:p="http://www.springframework.org/schema/p"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p" 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.xsd"
       >

    <bean name="helloworld" class="nocollection.HelloWorld">
        <property name="name" value="yaocheng"></property>
    </bean>

    <bean id="car1" class="nocollection.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="dazhong" index="1"></constructor-arg>
        <constructor-arg value="3000" type="double"></constructor-arg>
    </bean>
    <bean id="car2" class="nocollection.Car">
        <constructor-arg value="Audi" type="java.lang.String"></constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>
                <![CDATA[<beijing>]]>
            </value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>100</value>
        </constructor-arg>
    </bean>
    <bean id="person1" class="nocollection.Person">
        <property name="name" value="Tom"/>
        <property name="age" value="24"/>
        <!--
        <property name="car"><ref bean="car2"></ref></property>
        -->
        <property name="car">
            <bean class="nocollection.Car">
                <constructor-arg value="Ford"/>
                <constructor-arg value="changan"/>
                <constructor-arg value="200000" type="double"/>

            </bean>
        </property>
        <property name="car.maxSpeed" value="100"/>

    </bean>

    <bean id="person2" class="nocollection.Person">
        <constructor-arg value="Jerry"/>
        <constructor-arg value="25"/>
        <constructor-arg ref="car2"/>
    </bean>
    <bean id="person3" class="nocollection.Person">
        <constructor-arg value="Jerry"/>
        <constructor-arg value="25"/>
        <constructor-arg ref="car1"/>
        <property name="car.maxSpeed" value="50"/>
    </bean>


    <bean id="person4" class="collection.Person">
        <property name="name" value="Tom"/>
        <property name="age" value="26"/>
        <property name="cars">
            <list>
                <ref bean="car1"/>
                <ref bean="car2"/>
            </list>
        </property>
    </bean>

    <bean id="persona" class="collection.PersonMap">
        <property name="name" value="rosy"/>
        <property name="age" value="20"/>
        <property name="cars">
            <map>
                <entry key="A" value-ref="car1"></entry>
                <entry key="B" value-ref="car2"></entry>
            </map>
        </property>
    </bean>

    <bean id="datasource" class="collection.DataSource">
        <property name="properties">
            <props>
                <prop key="user">root</prop>
                <prop key="pwd">pwd</prop>
                <prop key="jdbcUrl">jdbc:mysql://test</prop>
                <prop key="driverClass">com.mysql.jdbc.Driver</prop>
            </props>
        </property>
    </bean>

    <util:list id="cars">
        <ref bean="car1"></ref>
        <ref bean="car2"></ref>
    </util:list>

    <bean id="person5" class="collection.Person">
        <property name="name" value="yao"></property>
        <property name="age" value="20"></property>
        <property name="cars" ref="cars"></property>
    </bean>

    <bean id="person6" class="collection.Person" p:name="yao" p:age="20" p:cars-ref="cars">
    </bean>


</beans>
相關文章
相關標籤/搜索