Spring系列(四)bean的作用域和自动装配

18 篇文章 3 订阅
13 篇文章 1 订阅

扩展:Spring系列学习汇总


一、bean的作用域
  • 在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。
  • 简单地讲,bean就是由IoC容器初始化、装配及管理的对象 .
  • 实体类:
public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
}
1.1、Singleton:单例

①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="person" class="dyj.entity.Person" scope="singleton">
        <property name="like" value="钓鱼"/>
        <property name="age" value="23"/>
        <property name="name" value="丁大大"/>
    </bean>
</beans>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person1 =  Context.getBean("person",Person.class);
        Person person2 =  Context.getBean("person",Person.class);
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person1==person2);
    }
}

③执行结果如下:
在这里插入图片描述

④总结:

  • scope为singleton时,Spring容器里面有且只有一个实例。
  • 不管你是否使用了该实例,在容器启动时就已经创建好了。
1.2、Prototype:原型

①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="person" class="dyj.entity.Person" scope="prototype">
        <property name="like" value="钓鱼"/>
        <property name="age" value="23"/>
        <property name="name" value="丁大大"/>
    </bean>
</beans>

②测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person1 =  Context.getBean("person",Person.class);
        Person person2 =  Context.getBean("person",Person.class);
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
        System.out.println(person1==person2);
    }
}

③执行结果:
在这里插入图片描述

④总结:

  • 当scope为prototype时,一个bean对应多个对象实例!
  • 在我们启动容器时并没有实例化,只有在使用时才会在Spring容器里实例化!
1.3、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实例将被销毁。
1.4、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也会被废弃掉。
二、Bean的自动装配
1.0、环境搭建

①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="car" class="dyj.entity.Car"/>
    <bean id="house" class="dyj.entity.House"/>

    <bean id="person" class="dyj.entity.Person">
        <property name="car" ref="car"/>
        <property name="house" ref="house"/>
        <property name="like" value="钓鱼"/>
        <property name="age" value="23"/>
        <property name="name" value="丁大大"/>
    </bean>
</beans>

②实体类:

public class Car {
    public void getName(){
        System.out.println("我是一辆车!");
    }
}
public class House {
    public void getName(){
        System.out.println("我是一栋房子!");
    }
}
public class Person {    
    private Car car;     
    private House house; 
    private String name; 
    private int age;     
    private String like; 
    private String high; 
}

③测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person =  Context.getBean("person",Person.class);
        person.getCar().getName();
        person.getHouse().getName();
    }
}

④执行结果:
在这里插入图片描述

⑤总结:

  • 搭建完成。
  • 可正常注入Bean。
1.1、byName:按名称自动装配

①修改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="car" class="dyj.entity.Car"/>
    <bean id="house" class="dyj.entity.House"/>

    <bean id="person" class="dyj.entity.Person" autowire="byName">
        <property name="like" value="钓鱼"/>
        <property name="age" value="23"/>
        <property name="name" value="丁大大"/>
    </bean>
</beans>

②执行结果:
在这里插入图片描述
③总结:

  • 证明可以通过 autowire=“byName” 根据名称来自动装配!
  • 前提是:bean id=“car” 的值和实体类中的属性名一致!
1.2、byType:按类型自动装配

①修改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 class="dyj.entity.Car"/>
    <bean class="dyj.entity.House"/>

    <bean id="person" class="dyj.entity.Person" autowire="byType">
        <property name="like" value="钓鱼"/>
        <property name="age" value="23"/>
        <property name="name" value="丁大大"/>
    </bean>
</beans>

②执行结果:
在这里插入图片描述

③总结:

  • 跟bean中的id无关,可去除!
  • 同一个bean只能有一个,否则报错!
1.3、contructor:构造器自动装配、ref:缺省情况下手动创建
  • 前面已经讲解过了,请参考!

Spring系列(二)Spring是如何通过IOC来创建对象的?


路漫漫其修远兮,吾必将上下求索~
如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧**转载请注明出处哦**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值