Bean属性配置

Spring在读取配置文件中bean的metadata后会构造一个个BeanDefination对象。后面Spring会根据这些BeanDefinition创建对象。在配置一个bean的时候我们可以配置下面这些元素:

元素 备注
class 设定类的全限定名
name 设置Bean的名称,可以设置多个用逗号分隔
id/name Bean的唯一标识符,全局只能有一个
scope 设置Bean是单例还是原型,默认单例
constructor arguments 构造方法入参,进行依赖注入
properties 进行依赖注入
autowiring mode 自动注入模式
lazy-initialization mode 只对scope为单列的bean生效,设置为true会在getBean()时才创建bean实例
initialization method 设定Bean的初始化方法
destruction method 设定Bean的销毁方法

除了通过配置信息来创建Bean,Spring还允许通过接口的方式创建用户在容器外部创建的对象。(通过DefaultListableBeanFactory类的registerSingleton方法和registerBeanDefinition方法)

In addition to bean definitions that contain information on how to create a specific bean, the ApplicationContextimplementations also permit the registration of existing objects that are created outside the container (by users). This is done by accessing the ApplicationContext’s BeanFactory through the getBeanFactory() method, which returns the BeanFactory DefaultListableBeanFactory implementation. DefaultListableBeanFactory supports this registration through the registerSingleton(..) and registerBeanDefinition(..) methods. However, typical applications work solely with beans defined through regular bean definition metadata.

虽然Spring容器还提供了registerSingleton(..)方法和registerBeanDefinition(..)来注册单例Bean,但是不建议自己使用这个方法,因为我们可能在依赖注入之后再注册了这个Bean。推荐使用配置BeanDefinition的方式来配置Bean。(其实这两个方法更多是Spring框架自己使用,在配置文件以外的一些地方再注册一些Bean到容器中)

关于上面表格中的id属性和name属性这边多说一句。

id和name属性都可以用来当做一个bean的标识符,他们两个的区别是id只能给这个bean指定一个标识符,而name属性可以同时给这个bean指定多个标识符(多个标识符之间用,隔开)。下面给出一个配置的列子

<!--经过下面的配置,bean1,bean2、name1、name2、alias1、alias2、alias3其实是一个Bean-->
<bean id="bean1,bean2" name="name1,name2" class="com.csx.demo.springdemo.service.MyBean1">
</bean>
<alias name="bean1,bean2" alias="alias1"/>
<alias name="name1" alias="alias2"/>
<alias name="name2" alias="alias3"/>

Bean命名

如果没有给Bean指定一个标识符,Spring容器会默认给这个Bean设置一个标识符。用户可以通过id和name两个属性来设置Bean的标识符,这些标识符需要在整个容器范围内唯一,同时name可以指定多个用逗号分隔。

实例化Bean

实例化Bean一般有四种方式:默认构造函数、待参数的构造函数、静态工厂类、实例方法创建Bean。需要我们注意的是Bean的实例化和依赖注入的区别。

  1. 默认构造函数方式
    <bean id="exampleBean" class="examples.ExampleBean"/>
  1. 带参数的构造函数
    <bean id="Hello2" class="com.csx.personal.web.services.HelloImpl2">
<constructor-arg index="0" value="chensongxia"/>
<constructor-arg index="1" value="zhaoru"/>
<constructor-arg index="2" ref="beanid"/>
</bean>
  1. 使用静态工厂类(不推荐使用)
    <bean id="bean3" class="cn.javass.spring.chapter2.HelloApiStaticFactory" factory-method="newInstance">
<constructor-arg index="0" value="Hello Spring!"/>
</bean>
<!-- 使用examples.ClientService这个类的createInstance方法创建bean -->
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/> public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
  1. 使用实例方法进行Bean创建
    <bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean>
<!-- 使用serviceLocator这个Bean的createClientServiceInstance方法创建Bean -->
<bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/>
<bean id="accountService" factory-bean="serviceLocator" factory-method="createAccountServiceInstance"/>
//类定义
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
private static AccountService accountService = new AccountServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
public AccountService createAccountServiceInstance() {
return accountService;
}
}

如果我们要通过xml的形式配置一个静态内部类,可以参考下面的例子

For example, if you have a class called SomeThing in the com.example package, and this SomeThing class has a static nested class called OtherThing, the value of the class attribute on a bean definition would be com.example.SomeThing$OtherThing.

Bean的Scope

Spring默认支持的Scope

Scopes 描述
singleton 整个IOC容器中只有一个Bean
prototype 每次请求都会生成新的Bean
request 每个HTTP request都会生成一个新的Bean,只对web系列的ApplicationContext生效
session 每个session范围内生成一个新的Bean,只对web系列的ApplicationContext生效
Application ServletContext范围内生成一个新的Bean,只对web系列的ApplicationContext生效
websocket --

singleton类型的bean定义,在一个容器中只存在一个实例,所有对该类型bean的依赖都引用这一单一实例。

scope为prototype的bean,容器在接受到该类型的对象的请求的时候(调用getBean方法),会每次都重新 生成一个新的对象给请求方,虽然这种类型的对象的实例化以及属性设置等工作都是由容器负责的,但是只要准备完毕,并且对象实例返回给请求方之后,容器就不 在拥有当前对象的引用,请求方需要自己负责当前对象后继生命周期的管理工作,包括该对象的销毁。也就是说,容器每次返回请求方该对象的一个新的实例之后, 就由这个对象“自生自灭”了。

单例Bean依赖原型Bean

当一个单例的Bean依赖一个原型Bean时,由于单例只初始化一次,所以拿到的原型Bean也只是我们第一次初始化时拿到的Bean,并不能达到我们想要的效果。此时我们可以使用Spring提供的look-up方式的注入来解决这个问题。

Request, Session, Application, and WebSocket Scopes

这三种Scope只对Web系列的ApplicationContext的生效。可以视同@RequestScope、@SessionScope和@ApplicationScope使之生效。

自定义Scope

Spring还支持自定义scope,需要时可以学些下这个特性。

Bean的扩展点(注意和容器扩展点的区别)

Spring提供了一系列接口让用户来自定义Bean的属性(注意和容器扩展点那一个章节的区别),主要的接口有:

  • Lifecycle Callbacks;
  • ApplicationContextAware and BeanNameAware;
  • Other Aware Interfaces;

生命周期回调

所谓生命周期交互就是指在容器创建Bean和销毁Bean之前做某些操作。在Spring中我们可以通过让Bean实现InitializingBean和DisposableBean接口,使用@PostConstruct和@PreDestroy以及通过如下的配置实现。

    <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>

如果我们给一个Bean同时配置了上面多种的初始化和销毁机制,那么他们的执行顺序如下:

初始化顺序

  1. Methods annotated with @PostConstruct (这个方法在构造函数之后执行,是最佳实践,可以和Spring API解耦)
  2. afterPropertiesSet() as defined by the InitializingBean callback interface(不建议使用,会耦合Spring API)
  3. A custom configured init() method(普通的xml配置文件中配置的初始化方法)

    析构顺序
  4. Methods annotated with @PreDestroy(建议使用)
  5. destroy() as defined by the DisposableBean callback interface (不建议使用)
  6. A custom configured destroy() method (普通的xml文件中配置的销毁方法)

Spring完整的初始化顺序

  1. 容器启动,实例化所有实现了BeanFactoyPostProcessor接口的类;这步会在任何普通Bean实例化之前加载;
  2. 实例化剩下的单例Bean,对这些Bean进行依赖注入;
  3. 如果Bean有实现BeanNameAware的接口那么对这些Bean进行调用;
  4. 如果Bean有实现BeanFactoryAware接口的那么对这些Bean进行调用;
  5. 如果Bean有实现ApplicationContextAware接口的那么对这些Bean进行调用;
  6. 如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessBeforeInitialization方法;
  7. 调用@PostConstruct注解的方法;
  8. 如果Bean有实现InitializingBean接口那么对这些Bean进行调用;
  9. 如果Bean配置有init属性,那么调用它属性中设置的方法;
  10. 如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessAfterInitialization方法;
  11. Bean正常的使用;
  12. 调用@PreDestroy标注的方法;
  13. 调用DisposableBean接口的destory方法;
  14. 调用Bean定义是指定的destroy-method方法;

LifeCycle和SmartLifeCycle接口

Spring提供了LifeCycle接口,实现了这个接口的Bean在Spring容器调用start()和stop()方法的时候能收到Spring的回调信息,分别调用这个Bean的start()和stop()方法。

Aware接口

Spring提供了很多aware接口让Bean来实现,提示IOC容器,这个Bean需要得到容器的某些组件或元素。

  • ApplicationContextAware
  • ApplicationEventPublisherAEwvaenrte
  • BeanClassLoaderAware
  • BeanFactoryAware
  • BeanNameAware
  • BootstrapContextAware:Typically available only in JCA aware ApplicationContext instances.
  • LoadTimeWeaverAware
  • MessageSourceAware
  • NotificationPublisherAwareSpring:Spring JMX notification publisher.
  • ResourceLoaderAware
  • ServletConfigAware
  • ServletContextAware

Spring系列.Bean简介的更多相关文章

  1. Spring 系列: Spring 框架简介 -7个部分

    Spring 系列: Spring 框架简介 Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级 ...

  2. Spring 系列: Spring 框架简介(转载)

    Spring 系列: Spring 框架简介 http://www.ibm.com/developerworks/cn/java/wa-spring1/ Spring AOP 和 IOC 容器入门 在 ...

  3. Spring系列__02IOC模块简介

    Spring的两大核心功能就是IOC和AOP,这篇文章主要介绍IOC. 简单来说,在面向对象思想下,A类中有一个B类的属性, 那么我们在创建A类时往往需要同时创建一个B类的对象,以便A类对其进行调用. ...

  4. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  5. 深入理解Spring系列之六:bean初始化

    转载 https://mp.weixin.qq.com/s/SmtqoELzBEdZLo8wsSvUdQ <深入理解Spring系列之四:BeanDefinition装载前奏曲>中提到,对 ...

  6. Spring核心技术(一)——IoC容器和Bean简介

    IoC容器和Bean简介 这章包括了Spring框架对于IoC规则的实现.Ioc也同DI(依赖注入).而对象是通过构造函数,工厂方法,或者一些Set方法来定义对象之间的依赖的.容器在创建这些Bean对 ...

  7. [JavaEE] IBM - Spring 系列: Spring 框架简介

    Spring AOP 和 IOC 容器入门 在这由三部分组成的介绍 Spring 框架的系列文章的第一期中,将开始学习如何用 Spring 技术构建轻量级的.强壮的 J2EE 应用程序.develop ...

  8. Spring源码系列 — Bean生命周期

    前言 上篇文章中介绍了Spring容器的扩展点,这个是在Bean的创建过程之前执行的逻辑.承接扩展点之后,就是Spring容器的另一个核心:Bean的生命周期过程.这个生命周期过程大致经历了一下的几个 ...

  9. java Spring系列之 配置文件的操作 +Bean的生命周期+不同数据类型的注入简析+注入的原理详解+配置文件中不同标签体的使用方式

    Spring系列之 配置文件的操作 写在文章前面: 本文带大家掌握Spring配置文件的基础操作以及带领大家理清依赖注入的概念,本文涉及内容广泛,如果各位读者耐心看完,应该会对自身有一个提升 Spri ...

随机推荐

  1. python3 的setter方法及property修饰

    #!/usr/bin/env pthon#coding:utf-8 class person(object): def __init__(self,name,sex,age,surface,heigh ...

  2. CPU亲和度

    CPU亲和度(CPU Affinity),就是将一个进程或者线程强制绑定在CPU的某一个core上运行. 参考:https://www.cnblogs.com/zhangxuan/p/6427533. ...

  3. Java实现 洛谷 P1085 不高兴的津津

    import java.io.*; import java.util.*; class Main{ public static void main(String args[]) { Scanner s ...

  4. Java实现 洛谷 P1426 小鱼会有危险吗

    import java.util.LinkedList; import java.util.Scanner; public class Main { private static Scanner ci ...

  5. PAT 组个最小数

    给定数字 0-9 各若干个.你可以以任意顺序排列这些数字,但必须全部使用.目标是使得最后得到的数尽可能小(注意 0 不能做首位).例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就 ...

  6. 一个Jmeter模拟上传文件接口的实例

    资料参考:https://blog.csdn.net/u010390063/article/details/78329373 项目中,避免不了要用到很多上传文件.图片的接口,那么碰到这类接口该如何进行 ...

  7. ReentrantReadWriteLock源码分析及理解

    本文结构 读写锁简介:介绍读写锁.读写锁的特性以及类定义信息 公平策略及Sync同步器:介绍读写锁提供的公平策略以及同步器源码分析 读锁:介绍读锁的一些常用操作和读锁的加锁.解锁的源码分析 写锁:介绍 ...

  8. spring Cloud服务注册中心Eureka集群

    spring Cloud服务注册中心Eureka集群配置: 在application.yml文件加以下配置: server: port: 8761 tomcat: uri-encoding: UTF- ...

  9. 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...

  10. 关于EF的五种状态

    最近半个多月没更新原因是在赶项目...在做项目的时候就会发现自己很多问题,也有很多东西要记录,今天就记录一下对ef新的认识. 也就是关于ef的五种状态管理. Unchanged(持久态) Midifi ...