Spring依赖注入(DI)

介绍

  • 依赖注入(Dependency Injection,DI)。
  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

编写实体类

  • student实体类
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 Properties info;
    private String wife;

    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 Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}
  • address实体类
public class Address {
    private String address;


    public Address() {
    }

    public Address(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

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

常量注入

 <bean id="student" class="com.wxt.pojo.Student">
     <property name="name" value="小明"/>
 </bean>

构造注入

  • 使用构造注入 实体类中一定要加入有参构造!!
  • 注意!!!添加了有参构造别忘记了无参构造

  • beans.xml有三种写法
    1.第一种

     通过index参数下标设置
     index的下标从0开始
    
<bean id="address" class="com.wxt.pojo.Address">
           
            <constructor-arg index="0" value="湖北"></constructor-arg>
        </bean>

2.根据参数名字来构造

 <bean id="address" class="com.wxt.pojo.Address">
           
           <constructor-arg name="address" value="湖北"></constructor-arg>
        </bean>

3.根据参数类型来构造

 <bean id="address" class="com.wxt.pojo.Address">
           
           <constructor-arg type="java.lang.String" value="湖北"></constructor-arg>
        </bean>

《在配置文件加载的时候,其中管理的对象就已经初始化了》

bean注入

  • ref 引用 ,引用bean里面的对象

        <bean id="address" class="com.wxt.pojo.Address" >
            <property name="address" value="湖北"></property>
           
        </bean>
       <bean id="student" class="com.wxt.pojo.Student">
        
           <property name="name" value="小吴"></property>

           <property name="address" ref="address"></property>
           </bean>

数组注入

 <bean id="student" class="com.wxt.pojo.Student">
      

         
           <!--数组注入-->
           <property name="books">
               <array>
                   <value>红楼梦</value>
                   <value>西游记</value>
                   <value>水浒传</value>
                   <value>三国演义</value>
               </array>
           </property>
           </bean>

List注入

 <property name="hobbys">
               <list>
                   <value>打游戏</value>
                   <value>听歌</value>
                   <value>看书</value>
               </list>
           </property>

map注入

<property name="card">
               <map>
                   <entry key="身份证" value="12345678971"></entry>
                   <entry key="银行卡" value="15175748915"></entry>
                   <entry key="饭卡" value="81844861874496"></entry>
               </map>
           </property>

set注入

  <property name="games">
               <set>
                   <value>LOL</value>
                   <value>CSGO</value>
                   <value>cf</value>
               </set>
           </property>

NULl注入

<property name="wife">
               <null></null>
           </property>

properties注入

 <property name="info">
               <props>
                   <prop key="学号" >20010508</prop>
                   <prop key="性别" ></prop>

               </props>
           </property>

p命名和c命名注入

p命名注入

导入约束

xmlns:p=“http://www.springframework.org/schema/p”

  • 我们使用上面的address来写beans.xml
    <bean id="Address" class="com.wxt.pojo.Address" p:address="湖北" ></bean>

P(属性: properties)命名空间 , 属性依然要设置set方法

c命名注入

导入约束

xmlns:c=“http://www.springframework.org/schema/c”

<bean id="Address" class="com.wxt.pojo.Address" c:address="湖北"></bean>

C(构造: Constructor)命名空间 , 属性依然要设置set方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值