Spring 学习笔记 (三):Bean

配置:

  • Id: The id attribute is a string that identifies the individual bean definition.
  • Class: The class attribute defines the type of the bean and uses the fully qualified
    classname.
  • Other properties: BeanDefinition objects
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

实例化:

  • 构造器实例化:Bean 配置中的 class 属性所指定 Bean 实例的实现类,使用 Bean 对应类中默认的无参构造方法
      <bean id="exampleBean" class="examples.ExampleBean"/>
    
      <bean name="anotherExample" class="examples.ExampleBeanTwo"/>
    
  • 静态工厂方式实例化:Bean 配置中的 class 属性所指定的不是 Bean 实例的实现类,而是静态工厂类,同时还需要使用 factory-method 属性指定静态工厂方法
      <bean id="clientService"
          class="examples.ClientService"
          factory-method="createInstance"/>
    
  • 实例工厂方式实例化:Bean 配置中的 class 属性所指定的不是 Bean 实例的实现类,而是通过 factory-bean 属性指向配置的实例工厂,使用 factory-method 属性确定使用工厂的哪个方法(配置文件中需要配置 2 个 Bean, 一个工厂 Bean,一个需要实例化的 Bean)
      <!-- the factory bean, which contains a method called createInstance() -->
      <bean id="serviceLocator" class="examples.DefaultServiceLocator">
          <!-- inject any dependencies required by this locator bean -->
      </bean>
    
      <!-- the bean to be created via the factory bean -->
      <bean id="clientService"
          factory-bean="serviceLocator"
          factory-method="createClientServiceInstance"/>
    

装配:

  • 基于 XML:
    1. 装配方式:
    2. 注入不同数据类型
      • 注入直接量(基本数据类型,字符串):注意特殊 xml 字符的转义
      • 引用其他 Bean 元素
      • 使用内部 Bean
      • 注入集合类型的属性
      • 特殊值:注入 null 和空字符串
      • 使用 p 命名空间实现属性注入:与设值注入关联
      • 使用 c 命名空间实现属性注入:与构造注入关联
  • 基于Annotation
    1. 定义 Bean:标注在实现类上
      • @Component:通用注解
      • @Repository:数据访问层注解(DAO 层)
      • @Service:业务层注解(Service 层)
      • @Controller:控制层注解(如 Spring MVC 的 Controller)
    2. Bean 组件装配:标注在属性变量,属性的 setter 方法,构造方法上
      • @Autowired:按照 Bean 的类型装配
      • @Qualifier:与 @Autowired 配合,修改装配方式为按 Bean 实例名称
      • @Resource:按照 Bean 的名字装配(name 及 type 都不设置,优先 name)
        1. name:Bean 实例名称
        2. type:Bean 实例类型
    3. 加载注解定义的 Bean (区别见:difference-between-contextannotation-config-and-contextcomponent-scan
  • 自动装配:beans-factory-autowire
    1. no:不使用自动装配
    2. byName:根据属性名自动装配
    3. byType:根据属性类型自动装配
    4. constructor:同 byType, 应用于构造器参数
    5. default(默认值):继承上级标签 的 default-autowired 属性

作用域:

  1. singleton:单例,默认值,适用于无会话状态的 Bean,如 Dao 组件,Service 组件,此类组件无线程安全问题,使用 singleton 方式可减少对象创建开销,提高运行效率
  2. prototype:原型,适用于需要保持会话状态的 Bean,此类组件存在线程安全问题,Spring 框架在每次获取该组件的时候都会创建一个新的实例
  3. request:Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
  4. session:Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
  5. application:Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
  6. websocket:Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

生命周期:见另一篇博文:https://www.cnblogs.com/uyiefiz/p/spring-bean-lifecycle.html

参考资料:

  1. 配置:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-definition
  2. 实例化:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-class
  3. 装配:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-dependencies
  4. 作用域:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-scopes
posted @ 2021-03-15 14:21  Uyiefiz  阅读(99)  评论(0)    收藏  举报