一篇Spring带你快速入门

Spring属性注入

2019-02-14  本文已影响11人  往事随风_0817

Spring属性注入

构造方法注入

使用<constructor-agr>设置属性值

//XML
<bean id="user" class="com.akwangl.demo4.User">
    <constructor-arg name="name" value="张三" />
    <constructor-arg name="age" value="23"/>
</bean>

//java
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 +
                '}';
    }
}

Set方法注入

使用set方法注入,在Spring配置文件中,通过<property>设置

//XML
<bean id="person" class="com.akwangl.demo4.Person">
    <property name="name" value="李四"/>
    <property name="age" value="32"/>
    <property name="cat" ref="cat"/>
</bean>

<bean id="cat" class="com.akwangl.demo4.Cat">
    <property name="name" value="ketty"/>
</bean>

//java
public class Person {
    private String name;
    private Integer age;
    private Cat cat;

    //get和set方法...

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", cat=" + cat +
                '}';
    }
}

public class Cat {
    private String name;

    //get和set方法...

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

注意:在注入对象属性时使用ref="BeanId"来引用

属性注入-p名称空间

Spring为了简化XML文件配置,从2.5开始引入一个新的p名称空间

语法

//首先在XML引入约束
xmlns:p="http://www.springframework.org/schema/p"

//XML
<bean id="person2" class="com.akwangl.demo4.Person" p:name="大黄" p:age="34" p:cat-ref="cat"/>

<bean id="cat2" class="com.akwangl.demo4.Cat" p:name="小黄"/>

//实体类请查阅上方实例

属性注入-SpEl注入

SpEl:spring expression language ,Spring表达式语言,对依赖注入进行简化

语法:#{表达式}

<bean id="xxx" value="#{表达式}">

//XML
<bean id="category" class="com.akwangl.demo4.Category">
    <!--注入字符串-->
    <property name="name" value="#{'服装'}"/>
</bean>


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

<bean id="product" class="com.akwangl.demo4.Product">
    <property name="name" value="#{'男装'}"/>
    <!--调用方法-->
    <property name="price" value="#{productInfo.calculatePrice()}"/>
    <!--对象的引用-->
    <property name="category" value="#{category}"/>
</bean>

//java
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 + '\'' +
                '}';
    }
}

public class ProductInfo {
    //随机价格

    public Double calculatePrice(){
        return Math.random() * 199;
    }
}

复杂类型的属性注入

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; // 属性类型

    //get和set...

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

数组类型的属性注入

//XML
<bean id="collectionBean" class="com.akwangl.demo5.CollectionBean">
    <!--数组类型-->
    <property name="arrs">
        <list>
            <value>aaa</value>
            <value>bbb</value>
            <value>ccc</value>
        </list>
    </property>
</bean>

List集合类型的属性注入

//XML
<bean id="collectionBean" class="com.akwangl.demo5.CollectionBean">
    <!--List集合的属性注入-->
    <property name="list">
        <list>
            <value>111</value>
            <value>222</value>
            <value>333</value>
        </list>
    </property>
</bean>

Set集合类型的属性注入

//XML
<bean id="collectionBean" class="com.akwangl.demo5.CollectionBean">
    <!--Set集合的属性注入-->
    <property name="set">
        <set>
            <value>ddd</value>
            <value>eee</value>
            <value>fff</value>
        </set>
    </property>
</bean>

Map集合类型的属性注入

//XML
<bean id="collectionBean" class="com.akwangl.demo5.CollectionBean">
    <!--Map集合的属性注入-->
    <property name="map">
        <map>
            <entry key="aaa" value="111"/>
            <entry key="bbb" value="222"/>
            <entry key="ccc" value="333"/>
        </map>
    </property>
</bean>

Properties类型的属性注入

//XML
<bean id="collectionBean" class="com.akwangl.demo5.CollectionBean">
    <!--Properties的属性注入-->
    <property name="properties">
        <props>
            <prop key="username">root</prop>
            <prop key="password">1234</prop>
        </props>
    </property>
</bean>
上一篇下一篇

猜你喜欢

热点阅读