Spring学习(4)——DI依赖注入

概念

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

构造器注入

set方式注入

  • 要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is

实体pojo类

  • Address.java
public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
  • Student.java
public class Student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private Properties 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> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", \naddress=" + address +
                ", \nbooks=" + Arrays.toString(books) +
                ", \nhobbies=" + hobbies +
                ", \ncard=" + card +
                ", \ngames=" + games +
                ", \ninfo=" + info +
                '}';
    }
}
  • beans.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
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="addr" class="site.duing.pojo.Address">
        <property name="address" value="中国"/>
    </bean>

    <bean id="student" class="site.duing.pojo.Student">
        <!--第一种,普通注入-->
        <property name="name" value="李磊"/>
        <!--第二种,Bean注入-->
        <property name="address" ref="addr"/>
        <!--第三种,数组注入-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
        <!--第四种,List注入-->
        <property name="hobbies">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>
        <!--第五种,Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="123456789"/>
                <entry key="银行卡" value="987654321"/>
            </map>
        </property>
        <!--第六种,Set注入-->
        <property name="games">
            <set>
                <value>CF</value>
                <value>lOL</value>
                <value>CS</value>
            </set>
        </property>
        <!--第七种,Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">0123456</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
</beans>
  • 测试
@Test
public void test() {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Student student = (Student) context.getBean("student");
    System.out.println(student.toString());
}

在这里插入图片描述

p命名和c命名注入

  • pojo类
public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  • p命名空间注入,导入约束 : xmlns:p=“http://www.springframework.org/schema/p”
  • 简化的set注入
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="site.duing.pojo.User" p:name="小飞" p:age="20"/>
  • c命名空间注入,导入约束 : xmlns:c=“http://www.springframework.org/schema/c”
  • 简化的构造器注入
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user2" class="site.duing.pojo.User" c:name="大飞" p:age="22"/>

bean的作用域

  • 在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象。
  1. Singleton(默认)
    • 当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:
 <bean id="user" class="site.duing.pojo.User" scope="singleton">
  • 测试
 @Test
 public void test03(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
     User user = (User) context.getBean("user");
     User user2 = (User) context.getBean("user");
     System.out.println(user==user2);
 }

结果为true

  1. Prototype
    • 当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:
<bean id="user" class="site.duing.pojo.User" scope="prototype"/>
  • 再用上述测试方法,结果是false
  1. 其余的request、session、application只能在web开发中使用到

下一篇:《Spring学习(5)——bean的自动装配》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值