Spring4 學習系列之——基於Bean的xml配置

基本的javabean對象,省略。。。java

注入方式有兩種:set方法和構造器注入spa

 

<!-- 配置bean set方式注入-->
    <bean id="helloWorld" class="com.dao.HelloWorld">
        <property name="name" value="Spring"></property>
    </bean>xml

 

<!--構造器注入  -->
    <bean id="car" class="com.dao.Car">
        <constructor-arg value="Audi"></constructor-arg>
        <constructor-arg value="Shanghai"></constructor-arg>
        <constructor-arg value="300000.0"></constructor-arg>
    </bean>對象

 

<bean id="person" class="com.dao.Person">
        <property name="name" value="Jrry"></property>
        <property name="sex" value="boy"></property>
        <!-- ref 表示對象關係間的引用 -->
        <!--  
        <property name="car" ref="car"></property>
        -->

        <!-- 內部bean的使用 -->
        <property name="car">
            <bean class="com.dao.Car">
                <constructor-arg value="BMW"></constructor-arg>
                <constructor-arg value="Beijinh"></constructor-arg>
                <constructor-arg value="500000.0"></constructor-arg>
            </bean>
        </property>
    </bean>get

//還能夠加載list map set 集合,進行級聯加載io

 

        //1.建立Spring的ioc容器對象
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        //2.從ioc獲取bean的實例class

        //下面兩種方式加載
       HelloWorld    world = (HelloWorld) context.getBean("helloWorld");容器

       HelloWorld    world = (HelloWorld) context.getBean(HelloWorld.class);
       world.hello();ioc

相關文章
相關標籤/搜索