1. 程式人生 > >屬性注入 5 種方式(XML)

屬性注入 5 種方式(XML)

package com.imooc.ioc.demo4;

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 +
                '}';
    }
}
<!--Bean的構造方法的屬性注入-->
<bean id="user" class="com.imooc.ioc.demo4.User">
    <constructor-arg name="name" value="張三" />
    <constructor-arg name="age" value="23"/>
</bean>
package com.imooc.ioc.demo4;

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

public class SpringDemo4 {

    @Test
    public void demo1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User)applicationContext.getBean("user");
        System.out.println(user);
    }
}
執行結果:
User{name='張三', age=23}

Ps:普通的用 value,bean用 ref。

package com.imooc.ioc.demo4;

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 +
                '}';
    }
}
package com.imooc.ioc.demo4;

public class Cat {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}
<!--Bean的set方法的屬性注入-->
<bean id="person" class="com.imooc.ioc.demo4.Person">
    <property name="name" value="李四"/>
    <property name="age" value="32"/>
    <property name="cat" ref="cat"/>
</bean>

<bean id="cat" class="com.imooc.ioc.demo4.Cat">
    <property name="name" value="ketty"/>
</bean>
package com.imooc.ioc.demo4;

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

public class SpringDemo4 {

    @Test
    public void demo2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person);
    }
}
執行結果:
Person{name='李四', age=32, cat=Cat{name='ketty'}}

<!--Bean的p名稱空間的屬性注入-->
<bean id="person" class="com.imooc.ioc.demo4.Person" p:name="大黃" p:age="34" p:cat-ref="cat"/>

<bean id="cat" class="com.imooc.ioc.demo4.Cat" p:name="小黃"/>
執行結果:
Person{name='大黃', age=34, cat=Cat{name='小黃'}}

package com.imooc.ioc.demo4;

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 + '\'' +
                '}';
    }
}
package com.imooc.ioc.demo4;

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 +
                '}';
    }
}
package com.imooc.ioc.demo4;

public class ProductInfo {

    public Double calculatePrice(){
        return Math.random() * 199;
    }
}
<!--Bean的SpEL的屬性注入-->
<bean id="category" class="com.imooc.ioc.demo4.Category">
    <property name="name" value="#{'服裝'}"/>
</bean>

<bean id="productInfo" class="com.imooc.ioc.demo4.ProductInfo"/>

<bean id="product" class="com.imooc.ioc.demo4.Product">
    <property name="name" value="#{'男裝'}"/>
    <property name="price" value="#{productInfo.calculatePrice()}"/>
    <property name="category" value="#{category}"/>
</bean>
package com.imooc.ioc.demo4;

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

public class SpringDemo4 {

    @Test
    public void demo3(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Product product = (Product)applicationContext.getBean("product");
        System.out.println(product);
    }
}
執行結果:
Product{name='男裝', price=155.43113186211818, category=Category{name='服裝'}}

package com.imooc.ioc.demo5;

import java.util.*;

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 +
                '}';
    }
}
<!--集合型別的屬性注入-->
<bean id="collectionBean" class="com.imooc.ioc.demo5.CollectionBean">
    <!--陣列型別-->
    <property name="arrs">
        <list>
            <value>aaa</value>
            <value>bbb</value>
            <value>ccc</value>
        </list>
    </property>

    <!--List集合的屬性注入-->
    <property name="list">
        <list>
            <value>111</value>
            <value>222</value>
            <value>333</value>
        </list>
    </property>

    <!--Set集合的屬性注入-->
    <property name="set">
        <set>
            <value>ddd</value>
            <value>eee</value>
            <value>fff</value>
        </set>
    </property>

    <!--Map集合的屬性注入-->
    <property name="map">
        <map>
            <entry key="aaa" value="111"/>
            <entry key="bbb" value="222"/>
            <entry key="ccc" value="333"/>
        </map>
    </property>

    <!--Properties的屬性注入-->
    <property name="properties">
        <props>
            <prop key="username">root</prop>
            <prop key="password">1234</prop>
        </props>
    </property>
</bean>
package com.imooc.ioc.demo5;

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

public class SpringDemo5 {
    @Test
    public void demo1(){
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

       CollectionBean collectionBean = (CollectionBean)applicationContext.getBean("collectionBean");

       System.out.println(collectionBean);
    }
}
執行結果:
CollectionBean{arrs=[aaa, bbb, ccc], list=[111, 222, 333], set=[ddd, eee, fff], map={aaa=111, bbb=222, ccc=333}, properties={password=1234, username=root}}