spring中bean的创建与注入

spring中bean的创建方式

有三种:

  1. 通过默认构造器创建bean,如果类中没有默认构造器(即构造器被重载且没有声明默认的),则对象无法创建。
 <bean name="source" class="pojo.Source">
        <property name="fruit" value="橙子"/>  <!--此处还有通过set注入操作-->
        <property name="sugar" value="多糖"/>
        <property name="size" value="超大杯"/>
    </bean>
  1. 通过普通工厂方法中创建对象,即使用某个类的方法创建对象,并注入spring容器,适用于外部导入的jar包类对象
/*
  模拟普通工厂类的静态方法存入对象
 */
public class StaticInterFactory {
    public static Source getObject(){
        return new Source();
    }
}
xml中配置如下:
  <bean id="interFactor" class="factory.InterFactoryDemo"></bean>
    <bean id="getObject" factory-bean="interFactor" factory-method="getObject"></bean>
  1. 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器),与第二种类似。

spring中注入bean的三种方式:

  1. 通过构造器注入
    :构造函数注入,因为需要默认的构造函数生成对象,然而重写了构造函数,所以就需要如下标签
    使用标签:construct-args
    标签类型:type;指定要注入的数据类型
    index:参数索引,从0开始
    name:用于指定名称的参数赋值,最常用
    ref;外部对象的引用
    优势:在获取Bean对象时,注入数据是必须的操作,否则对象无法创建成功
    弊端:改变了Bean对象的实例化方式,即对象实例化必须有数据
<bean id="sCope" class="pojo.SourceCopy">
        <constructor-arg name="count" value="1" ></constructor-arg>
        <constructor-arg name="fruit" value="苹果"></constructor-arg>
        <constructor-arg name="sugar" value="加糖"></constructor-arg>
        <constructor-arg name="date"  ref="now"></constructor-arg>
    </bean>
  1. 通过setter方法注入,常用操作
    在对应的pojo类中定义好get与set方法后,在xml中配置如下
<bean id="kenny" class="com.spring.entity.Instrumentalist">
    <property name="song" value="May Rain"/>
</bean>
  1. 通过注解注入,本质还是封装了以setter注入的方式,spring框架一直以注解为标准方式

Bean对象的生命周期

  1. bean对象的生命周期
    单例对象:出生:当容器创建时,对象出生;活着:只要容器还在,对象就存在;死亡:当容器消失,对象死亡
    单例对象的生命周期和容器相同。
    多例对象;使用对象时,spring为我们创建,对象只要在使用,就一直活着,当对象长期不用,且没有别的对象的引用
    ,由jvm垃圾回收机制回收。
  2. bean的作用范围调整,bean标签中的scope属性:作用:用于指定bean的作用范围
    取值:singleton:单例(默认值)
    prototype:多例
    request;作用于web应用的请求范围
    session:作用于web应用的会话范围
    globle-session:作用于集群环境的会话范围(全局会话范围)

在这里插入图片描述. 1.Spring对Bean进行实例化;

2.Spring将值和对Bean的引用注入进Bean对应的属性中;

3.如果Bean实现了BeanNameAware接口,Spring将Bean的ID传递给setBeanName()接口方法;

4.如果Bean实现了BeanFactoryAware接口,Spring将调用setBeanFactory()接口方法,将BeanFactory容器实例传入;

5.如果Bean实现了ApplicationContextAware接口,Spring将调用setApplicationContext()接口方法,将应用上下文的引用传入;

6.如果Bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessBeforeInitialization()接口方法;

7.如果Bean实现了InitializingBean接口,Spring将调用它们的afterPropertiesSet()接口方法。类似的,如果Bean使用init-method声明了初始化方法,该方法也会被调用。

8.如果Bean实现了BeanPostProcessor接口,Spring将调用它们的postProcessAfterInitialization()方法;

9.此时此刻,Bean已经准备就绪,可以被使用了。它们将一直驻留在Spring容器中,直到容器被销毁;

10.如果Bean实现了DisposableBean接口,Spring将调用它的destroy()接口方法。同样,如果Bean使用destroy-method声明了销毁方法,该方法也会被调用;

初始化Bean与销毁bean

1. 为Bean定义初始化和销毁操作,只需要使用init-method和destroy-method参数来配置<bean>元素。init-method属性指定了在初始化Bean时要调用的方法。destroy-method属性指定了Bean从容器移除之前要调用的方法。     
2. 还可以定义默认的init-method和destroy-method。如果上下文中Bean定义了名字相同的初始化和销毁方法,我们可以统一配置default-init-method和default-destroy-method:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="
    	http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context-3.2.xsd
    	http://www.springframework.org/schema/util
    	http://www.springframework.org/schema/util/spring-util-3.2.xsd
    	http://www.springframework.org/schema/jee
    	http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    	http://www.springframework.org/schema/tx
    	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    	http://www.springframework.org/schema/data/jpa
    	http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    	http://www.springframework.org/schema/mvc
    	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
        default-init-method="turnOnTheLight" 
        default-destroy-method="turnOffTheLight">
    <!--Bean declarations go here -->
</beans>

但是这种方式需要对应的类需要实现Spring的InitializingBean和DisposableBean接口。InitializingBean声明了一个afterPropertiesSet()方法作为初始化方法。DisposableBean声明了一个destroy()方法作为销毁方法。在Spring容器中无需任何配置。
但这种方法的缺点显而易见了:实现Spring的接口意味着Bean与Spring的API产生了耦合。所以这种方法并不推荐!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值