Loading

Spring系列.Bean简介

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还允许通过调用API的方式创建用户在容器外部创建的对象。(通过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="xx"/>
        <constructor-arg index="1" value="xx"/>
        <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
posted @ 2020-07-01 16:58  程序员自由之路  阅读(869)  评论(1编辑  收藏  举报