Spring学习笔记4:依赖注入 DI

一、概述

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

二、Set注入

pojo类

 public class Address {
 
     private String address;
 
     public String getAddress() {
         return address;
    }
 
     public void setAddress(String address) {
         this.address = address;
    }
 }
public class Student {
 
     private String name;    //常量
     private Address address;    //Bean
     private String[] books;    //数组
     private List<String> hobbys;    //List
     private Map<String,String> card;    //Map
     private Set<String> games;    //set
     private String wife;    //Null
     private Properties info;    //Properties
 
     public void setName(String name) {this.name = name;}
     public void setAddress(Address address) {this.address = address;}
     public void setBooks(String[] books) {this.books = books;}
     public void setHobbys(List<String> hobbys) {this.hobbys = hobbys;}
     public void setCard(Map<String, String> card) {this.card = card;}
     public void setGames(Set<String> games) {this.games = games;}
     public void setWife(String wife) {this.wife = wife;}
     public void setInfo(Properties info) {this.info = info;}

     public void show(){
         System.out.println("name="+ name + ",address="+ address.getAddress()
                 + ",books="
        );
         for (String book:books){
             System.out.print("<<"+book+">>\t");
        }
         System.out.println("\n爱好:"+hobbys);
         System.out.println("card:"+card);
         System.out.println("games:"+games);
         System.out.println("wife:"+wife);
         System.out.println("info:"+info);
    }
 }

1.常量注入

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

2.Bean注入

 <bean id="addr" class="com.pojo.Address">
     <property name="address" value="广东"/>
 </bean>
 
 <bean id="student" class="com.pojo.Student">
     <property name="name" value="xiaoyi"/>
     <property name="address" ref="addr"/> 通过引用ref注入bean
 </bean>

3.数组注入

 <bean id="student" class="com.pojo.Student">
     <property name="name" value="xiaoyi"/>
     <property name="address" ref="addr"/>
     <property name="books">
         <array>
             <value>java</value>
             <value>C语言</value>
             <value>算法</value>
         </array>
     </property>
 </bean>

4.List注入

 <property name="hobbys">
     <list>
         <value>听歌</value>
         <value>看电影</value>
         <value>爬山</value>
     </list>
 </property>

5.Map注入

 <property name="card">
     <map>
         <entry key="中国邮政" value="123456"/>
         <entry key="建设" value="321654"/>
     </map>
 </property>

6.set注入

 <property name="games">
     <set>
         <value>1</value>
         <value>2</value>
         <value>3</value>
     </set>
 </property>

7.Null注入

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

8.Properties注入

 <property name="info">
     <props>
         <prop key="学号">123456</prop>
         <prop key="性别">男</prop>
         <prop key="姓名">xiaoyi</prop>
     </props>
 </property>

三、p命名和c命名注入

实体类:

 public class User {
     private String username;
     private int age;
 
     public void setUsername(String username) {
         this.username = username;
    }
     public void setAge(int age) {
         this.age = age;
    }
     @Override
     public String toString() {
         return "User{" +
                 "username='" + username + '\'' +
                 ", age=" + age +
                 '}';
    }
 }

p命名:

p命名空间是set注入的一种快捷实现方式,想要使用p命名空间注入,需要注意一下几点。

  • 实体类中必须有set方法;
  • 实体类中必须有无参构造器(默认存在);
  • 必须导入p命名空间注入方式依赖。

 1、P命名空间注入 : 需要在头文件中加入约束文件

 导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.pojo.User" p:name="xiaoyi" p:age="18"/>

c命名 

c命名空间是构造器注入的一种快捷实现方式,想要使用c命名空间,需要注意一下几点。

  • 实体类中必须存在有参构造器
  • 必须导入c命名空间注入方式依赖。

2、c 命名空间注入 : 需要在头文件中加入约束文件

 导入约束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.pojo.User" c:name="xiaoyi" c:age="18"/>

四、Bean的作用域

作用域介绍四种:

Singleton

当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,
并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton
是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,
他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺
省作用域。要在XML中将bean定义成singleton,可以这样配置:

 <bean id="user" class="com.pojo.User" scope="singleton">

Prototype

当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的
bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建
容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每
次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用
域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,
可以这样配置: 

<bean id="user" class="com.pojo.User" scope="prototype"/>  
  或者
 <bean id="user" class="com.pojo.User" singleton="false"/>

Request

当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;
即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅
在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

 <bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
针对每次HTTP请求,Spring容器会根据loginAction bean的定义创建一个全新的LoginAction 
bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心
的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到
这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。

 

Session

当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。
该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

 <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的
userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。
与request作用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session
中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。
当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值