Spring Bean管理--XML方式(下)——(四)

一.構造方法注入

通過構造方法注入Bean的屬性值或依賴的對象,它保證了Bean實例在實例化後就可以使用

構造器注入在<constructor-arg>元素裏聲明的屬性

1.新建類User,並Generate toString()方法
public class User {
    private String name;
    private Integer age;

    public User(String name,Integer age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
2.在applicationContext.xml裏面編寫:
<!--Bean的構造方法的屬性注入-->
<bean id="user" class="com.imooc.ioc.demo4.User">
<constructor-arg name="name" value="張三" />
<constructor-arg name="age" value="23"/>
</bean>
3.新建測試類SpringDemo4
  @Test
    public void demo1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User)applicationContext.getBean("user");
        System.out.println(user);
    }

二.set方法屬性注入

1.新建類User,並setter,getter方法,Generate toString()方法
public class Person {
    private String name;
    private Integer age;

    private Cat cat;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", cat=" + cat +
                '}';
    }
}
2.在applicationContext.xml裏面編寫:
<!--Bean的set方法的屬性注入->
<bean id="person" class="com.imooc.ioc.demo4.Person">
<property name="name" value="李四"/>
<property name="age" value="32"/>
</bean>
3.新建測試類SpringDemo4
  @Test
    public void demo2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person);
    }

三.p名稱空間屬性注入

使用p命名空間,爲了簡化XML文件配置,Spring從2.5開始引入一個新的p名稱空間

P:<屬性名>="xxx"引入常量值

p:<屬性名>-ref="xxx"引用其他Bean對象

1.在applicationContext.xml裏面引入p
xmlns:p="http://www.springframework.org/schema/p"
<!--Bean的p名稱空間的屬性注入-->
<bean p:cat-ref="cat" p:age="34" p:name="大黃" class="com.imooc.ioc.demo4.Person" id="person"/>
<bean p:name="小黃" class="com.imooc.ioc.demo4.Cat" id="cat"/>

四.SpEL屬性注入

SqEL:spring expression language , spring 表達式語言 ,對依賴注入進行簡化
語法:#{表達式}
<bean id="" value"#{表達式}">
如:
#{ ‘hello’ } :使用字符串
#{ beanId} :使用另一個bean
#{beanId.content.toUpperCase()} :使用指定名屬性,並使用方法
#{ T(java.lang.Math).PI} :使用靜態字段方法

1.新建類Category ,得到getter方法,Generate toString()方法
public class Category {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Category{" +
                "name='" + name + '\'' +
                '}';
    }
}

2.新建類Product ,並setter,getter方法,Generate toString()方法
public class Product {
    private String name;
    private Double price;

    private Category category;

    public String getName() {
        return name;
    }

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

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                ", category=" + category +
                '}';
    }
}
3.在applicationContext.xml裏面編寫

<bean class="com.imooc.ioc.demo4.Category" id="category">

<property name="name" value="#{'服裝'}"/>

</bean>

<bean class="com.imooc.ioc.demo4.Product" id="product">

<property name="name" value="#{'男裝'}"/>

<property name="price" value="#{productInfo.calculatePrice()}"/>

<property name="category" value="#{category}"/>

</bean>
4.新建測試類SpringDemo4
@Test
    public void demo3(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Product product = (Product)applicationContext.getBean("product");
        System.out.println(product);
    }

五.複雜類型屬性注入

1.新建CollectionBean類,getter和setter方法,generate toString()方法:

public class CollectionBean {
    private String[] arrs; //數組類型

    private List<String> list; //List集合類型

    private Set<String> set;  //Set集合類型

    private Map<String,Integer> map;  //Map集合類型

    private Properties properties; //屬性類型

    public String[] getArrs() {
        return arrs;
    }

    public void setArrs(String[] arrs) {
        this.arrs = arrs;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, Integer> getMap() {
        return map;
    }

    public void setMap(Map<String, Integer> map) {
        this.map = map;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "CollectionBean{" +
                "arrs=" + Arrays.toString(arrs) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                '}';
    }
}

2.新建測試demo3:

@Test
    public void demo3(){
        //創建Spring的工廠
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通過工廠獲得類
        CollectionBean collectionBean =(CollectionBean) applicationContext.getBean("collectionBean");
        System.out.println(collectionBean);
    }

3.五種注入方式寫在applicationContext.xml裏面,寫在<bean>標籤裏面:

<bean class="com.imooc.ioc.demo1.CollectionBean" id="collectionBean"></bean>
1)數組類型屬性注入
<!--數組類型-->
                <property name="arrs">
                        <list>
                                <value>aaa</value>
                                <value>bbb</value>
                                <value>ccc</value>
                                <value>ddd</value>
                        </list>
                </property>
2)List集合類型屬性注入
<!--List集合的屬性注入-->
                <property name="list">
                        <list>
                                <value>111</value>
                                <value>222</value>
                                <value>333</value>
                        </list>
                </property>
3)Set集合類型屬性注入
<!--Set集合的屬性注入-->
                <property name="set">
                        <list>
                                <value>ddd</value>
                                <value>eee</value>
                                <value>fff</value>
                        </list>
                </property>
4)Map集合類型屬性注入
 <!--Map集合的屬性注入-->
                <property name="map">
                        <map>
                                <entry value="111" key="aaa"></entry>
                                <entry value="222" key="bbb"></entry>
                                <entry value="333" key="ccc"></entry>
                        </map>
                </property>
5)Properties類型屬性注入
 <!--Properties的屬性注入-->
                <property name="properties">
                       <props>
                               <prop key="username">root</prop>
                               <prop key="password">1234</prop>
                       </props>
                </property>
6)運行demo3結果:
CollectionBean{arrs=[aaa, bbb, ccc, ddd], list=[111, 222, 333], set=[ddd, eee, fff], map={aaa=111, bbb=222, ccc=333}, properties={password=1234, username=root}}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章