Spring(二):初始值赋值

依赖注入

1.构造器注入

  • 见spring简介最后一大点。

2.Set方式注入

  • 依赖注入:Set注入

    • 依赖:bean对象的创建依赖于容器。

    • 注入:bean对象的所有属性,由容器注入。

  • bean ,里面属性的注入见下文xml

package com.rzp.pojo;
​
import java.util.*;
​
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String www;
    private Properties info;
​
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", www='" + www + '\'' +
                ", info=" + info +
                '}';
    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public Address getAddress() {
        return address;
    }
​
    public void setAddress(Address address) {
        this.address = address;
    }
​
    public String[] getBooks() {
        return books;
    }
​
    public void setBooks(String[] books) {
        this.books = books;
    }
​
    public List<String> getHobbys() {
        return hobbys;
    }
​
    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }
​
    public Map<String, String> getCard() {
        return card;
    }
​
    public void setCard(Map<String, String> card) {
        this.card = card;
    }
​
    public Set<String> getGames() {
        return games;
    }
​
    public void setGames(Set<String> games) {
        this.games = games;
    }
​
    public String getWww() {
        return www;
    }
​
    public void setWww(String www) {
        this.www = www;
    }
​
    public Properties getInfo() {
        return info;
    }
​
    public void setInfo(Properties info) {
        this.info = info;
    }
}
​

 

  • xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.rzp.pojo.Address"/>
    <bean id="student" class="com.rzp.pojo.Student">
        <!--普通值注入-->
        <property name="name" value="rzp"/>
        <!--也可以把value写在两个标签中间
        <property name="name">
            <value>rzp</value>
        </property>-->
​
​
        <!--bean注入-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property><!--List-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>代码</value>
            </list>
        </property><!--Map-->
        <property name="card">
            <map>
                <entry key="学生卡" value="0010"/>
                <entry key="校园卡" value="0012110"/>
            </map>
        </property><!--Set-->
        <property name="games">
            <set>
                <value>MHW</value>
                <value>DMC</value>
            </set>
        </property>
        
        <!--null-->
        <property name="www">
            <null/>
        </property><!--Properties-->
        <property name="info">
            <props>
                <prop key="kclassname">1班</prop>
                <prop key="name">rzp</prop>
            </props>
        </property>
    </bean></beans>
 

 

3.拓展方式注入

  • c命名空间和p命名空间:

  • 要在beans标签中加入:

       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间,参数注入-->
    <bean id="user" class="com.rzp.pojo.User" p:name="rzp" p:age="18"/>
    <!--c命名空间,构造器注入-->
    <bean id="user2" class="com.rzp.pojo.User" c:name="rzp1" c:age="1"/>
​
​
</beans>

 

  • p命名空间就是对参数注入。

  • c命名空间就是使用构造器注入,必须要有构造器。

posted @ 2020-04-04 23:11  renzhongpei  阅读(372)  评论(0编辑  收藏  举报