3.Bean的实例化及作用域

1.Bean的配置

如果把Spring看做一个大型工厂,则Spring容器中的Bean就是该工厂的产品。要想使用这个工厂生产和管理Bean,就需要在配置文件中告诉它需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起.
小提示: Bean的本质就是Java中的类,而Spring中的Bean其实就是对实体类的引用,来生产Java类对象,从而实现生产和管理Bean.
在这里插入图片描述
在配置文件中,通常一个普通的Bean只需要定义id(或name)和class 两个属性即可,定义Bean的方式如下所示:

<?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="bean1" class="com.itheima.Bean1" />
            <bean name="bean2" class="com.itheima.Bean2" />
    </beans>

小提示:如果在Bean中未指定id和name,则Spring会将class值当作id使用。

2.Bean的实例化

在面向对象的程序中,想要使用某个对象,就需要先实例化这个对象。同样,在Spring中,要想使用容器中的Bean,也需要实例化Bean。实例化Bean有三种方式,分别为构造器实例化、静态工厂方式实例化和实例工厂方式实例化(其中最常用的是构造器实例化)

2.1 构造器实例化

Bean1.java

 public class Bean1 {
    public Bean1(){
            }
}

beans1.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="bean1" class="cn.ssm.bean.Bean1"/>
</beans>

BeanTest1

public class BeanTest1 {
    public static void main(String[] args){
//        String xmlpath="cn\\ssm\\bean\\beans1.xml";
        String xmlpath="cn//ssm//bean//beans1.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
        Bean1 bean1=(Bean1) applicationContext.getBean("bean1");
        System.out.print(bean1);
    }
}

2.2 静态工厂方式实例化

该方式要求开发者创建一个静态工厂的方法来创建|Bean的实例,其Bean配置中 class属性所指定的 再是Bean实例的实现类,而是静态工厂类,同时还需要使用factory-method属性来指定所创建的静态工厂方法。

Bean2.java

public class Bean2 {
}

静态工厂类:MyBean2Factory

public class MyBean2Factory {
    public static Bean2 createBean(){
        return new Bean2();
    }
}

beans2.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="bean2" class="cn.ssm.beanfactory.MyBean2Factory" factory-method="createBean"/>
</beans>

BeanTest2

public class BeanTest2 {
    public static void main(String[] args){
//        String xmlpath="cn\\ssm\\bean\\beans2.xml";
        String xmlpath="cn//ssm//beanfactory//beans1.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
        Bean2 bean2=(Bean2) applicationContext.getBean("bean2");
        System.out.print(bean2);
    }
}

2.3.实例工厂方式实例化

此种方式的工厂类中,不再使用静态方法创建Bean实例,而是采用直接创建Bean实例的方式。同时,在配置文件中,需要实例化的Bean也不是通过class属性直接指向的实例化类,而是通过factory-bean属性指向配置的实例工厂,然后使用factory-method属性确定使用工厂中的哪个方法。
Bean3.java

public class Bean3 {
}

MyBean3Factory

public class MyBean3Factory {
    public MyBean3Factory(){
        System.out.println("bean工厂实例化中");
    }
    public Bean3 createBean(){
        return new Bean3();
    }
}

bean3.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="myBean3Factory" class="cn.ssm.bean3.MyBean3Factory"/>
    <!--使用factory-bean属性指向配置的实例工厂,
    使用factory-method属性确定使用工厂中的哪个方法-->
    <bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"/>
</beans>

Bean3Test.java

public class Bean3Test {
    public static void main(String[] args){
//        String xmlpath="cn\\ssm\\bean\\beans1.xml";
        String xmlpath="cn//ssm//bean3//beans3.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
        Bean3 bean3=(Bean3) applicationContext.getBean("bean3");
        System.out.print(bean3);
    }
}

3.Bean的作用域

3.1 作用域的种类

Spring 4.3中为Bean的实例定义了7种作用域,如下表所示:
在这里插入图片描述

3.2 singleton作用域

singleton是Spring容器默认的作用域,当Bean的作用域为singleton时,Spring容器就只会存在一个共享的Bean实例,并且所有对Bean的请求,只要id与该Bean的id属性相匹配,就会返回一个Bean实例。singleton作用域对于无会话状态的Bean(如Dao组件、Service组件)来说,是最理想的选择。
案例:
bean4.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="bean1" class="cn.ssm.bean.Bean1" scope="singleton"/>
</beans>

ScopeTest

public class ScopeTest {
    public static void main(String[] args){
        String xmlpath="cn//ssm//bean4//beans4.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlpath);
        System.out.println(applicationContext.getBean("bean1"));
        System.out.println(applicationContext.getBean("bean1"));
    }
}

3.3 prototype作用域

对需要保持会话状态的Bean(如Struts 2的Action类)应该使用prototype作用域。在使用prototype作用域时,Spring容器会为每个对该Bean的请求都创建一个新的实例。
在Spring配置文件中,同样使用元素的scope属性,将Bean的作用域定义成prototype

4.Bean的生命周期

Spring容器可以管理singleton作用域的Bean的生命周期,在此作用域下,Spring能够精确地知道该Bean何时被创建, 何时初始化完成 以及何时被销毁。对于prototype作用域的Bean,Spring只负责创建,当容器他好地Bean实例后,Bean的实例就交给客户端代码来管理,Spring容器将不再跟踪生命周期。每次客户端请求prototype作用域的Bean时,Spring容器都会创建一个新的实例,并且不会管那些被配置成prototype作用域的Bean的生命周期。
了解Spring中Bean的生命周期的意义就在于,可以利用Bean在其存活期间的特定时刻完成一些相关操作。这种时刻可能有很多,但一般情况下,常会在Bean的postinitiation(初始化后)和predestruction(销毁前)执行一些相关操作。

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聊城云在天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值