spring的基於XML方式的屬性注入


1.掌握spring的屬性注入的方法:

1.1構造方法注入普通值---------<constructor-arg>標簽的使用

首先新建一個類

package spring.day1.demo3;

public class car {

    private String name;
    private double price;

    public car(String name, Double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "car [name=" + name + ", price=" + price + "]";
    }

}
View Code

 在編寫applicationContext2.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- =======================引入spring的關於bean的約束 ========================= -->
<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- ================spring屬性注入=============================== -->
    <!-- 構造方法注入 -->
    <bean id="car" class="spring.day1.demo3.car">
        <constructor-arg name="name" value="寶馬"></constructor-arg>
        <constructor-arg name="price" value="500000"></constructor-arg>
    </bean>
    
</beans>
View Code

在編寫測試類SpringDemo3

package spring.day1.demo3;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springDemo3 {

    @Test
    public void demo1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
        car bean = (car) applicationContext.getBean("car");
        System.out.println(bean);
    }
}
View Code

運行結果如下

1.2set方法注入普通值和對象屬性(ref的使用)------<property>標簽的使用

新建一個類car2

package spring.day1.demo3;

public class car2 {

    private String name;
    private double price;
    
    public void setName(String name) {
        this.name = name;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "car2 [name=" + name + ", price=" + price + "]";
    }
        
}
View Code

新建一個Employee類

package spring.day1.demo3;

public class employee {
    private String name;
    private car2 car;

    public void setName(String name) {
        this.name = name;
    }

    public void setCar(car2 car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "Employee [name=" + name + ", car=" + car + "]";
    }
}
View Code

在applicationContext2.xml中加入以下

<!-- set方法對car2注入普通值 -->
    <bean id="car2" class="spring.day1.demo3.car2">
        <property name="name" value="奔馳"></property>
        <property name="price" value="600000"></property>
    </bean>
    
    <!-- set方法注入對象 -->
    <bean id="employee" class="spring.day1.demo3.employee">
        <property name="name" value="張三"></property>
        <property name="car" ref="car2"></property>
    </bean>
View Code

在測試類SpringDemo3加入以下

@Test
    public void demo2() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
        employee bean = (employee) applicationContext.getBean("employee");
        System.out.println(bean);
    }
View Code

運行截圖如下

1.3SPEL方式注入普通值和對象值(Spring3.0以后)

SpEL:Spring Expression Language,Spring的表達式語言。

語法格式:#{SPEL}

新建一個類carInfo

package spring.day1.demo3;

public class carInfo {
    private String name;
    private double price;

    public String getName() {
        return "小三輪兒";
    }

    public double getPrice() {
        return Math.random()*1000;
    }

}
View Code

修改applicationContext2.xml為如下

<?xml version="1.0" encoding="UTF-8"?>
<!-- =======================引入spring的關於bean的約束 ========================= -->
<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- ================spring屬性注入=============================== -->
    <!-- 構造方法注入 -->
    <bean id="car" class="spring.day1.demo3.car">
        <constructor-arg name="name" value="寶馬"></constructor-arg>
        <constructor-arg name="price" value="500000"></constructor-arg>
    </bean>

    <!-- set方法對car2注入普通值 -->
    <!-- <bean id="car2" class="spring.day1.demo3.car2">
        <property name="name" value="奔馳"></property>
        <property name="price" value="600000"></property>
    </bean> -->

    <!-- set方法注入對象 -->
    <!-- <bean id="employee" class="spring.day1.demo3.employee">
        <property name="name" value="張三"></property>
        <property name="car" ref="car2"></property>
    </bean> -->

    <!-- SPEL方式 -->
    <bean id="carInfo" class="spring.day1.demo3.carInfo"></bean>
    
    <bean id="car2" class="spring.day1.demo3.car2">
        <property name="name" value="#{carInfo.name}"></property>
        <property name="price" value="#{carInfo.getPrice()}"></property>
    </bean>

</beans>
View Code

在測試類SpringDemo3加入以下

@Test
    public void demo3() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
        car2 bean = (car2) applicationContext.getBean("car2");
        System.out.println(bean);
    }
View Code

運行截圖如下

 1.4P名稱空間方式注入普通值和對象(Spring2.5以后)了解

2.集合屬性注入(了解)

1.新建CollectionBean類

2.在applicationContext2.xml中加入如下代碼

3.新建測試類測試

<!-- =======================spring集合屬性的注入============================ -->
    <bean id="collectionBean" class="zcc.spring.demo5.CollectionBean">
        <!-- 數組類型 -->
        <property name="arrs">
            <list>
                <value>張三</value>
                <value>李四</value>
                <value>王麻子</value>
            </list>
        </property>
        <!-- List集合類型 -->
        <property name="list">
            <list>
                <value>一號</value>
                <value>二號</value>
                <value>三號</value>
            </list>
        </property>
        <!-- Set集合類型 -->
        <property name="set">
            <set>
                <value>a</value>
                <value>b</value>
                <value>c</value>
            </set>                
        </property>
        <!-- Map集合類型 -->
        <property name="map">
            <map>
                <entry key="數字" value="1"></entry>
                <entry key="性別" value="男"></entry>
            </map>                
        </property>
    </bean>
View Code
package zcc.spring.demo5;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 * 集合屬性的注入
 */
public class CollectionBean {
    private String []arrs;
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    public void setArrs(String[] arrs) {
        this.arrs = arrs;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public void setSet(Set<String> set) {
        this.set = set;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    @Override
    public String toString() {
        return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map
                + "]";
    }
    
}
View Code
package zcc.spring.demo5;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/*
 * 集合類型的屬性注入
 */
public class SpringDemo5 {

    @Test
    public void demo1() {
        //創建spring的工廠來實例化xml中所有的bean對象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        CollectionBean bean = (CollectionBean) applicationContext.getBean("collectionBean");
        System.out.println(bean);
    }
    
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM