zoukankan      html  css  js  c++  java
  • Spring Bean

    Bean定义

    bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象。这些 bean 是由IOC容器读取Bean的配置元数据创建的。

    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-3.0.xsd">
    
       <bean id="..." class="..." scope="...">
           <!--  -->
       </bean>
    
       <bean id="..." class="..." lazy-init="true">
           <!--  -->
       </bean>
    
       <bean id="..." class="..." init-method="...">
           <!--  -->
       </bean>
    
       <bean id="..." class="..." destroy-method="...">
           <!--  -->
       </bean>
    
       <!-- 其他配置 -->
    
    </beans>

    Bean 与Spring容器关系

    Spring 配置元数据

    Spring IoC 容器完全由实际编写的配置元数据的格式解耦。有下面三个重要的方法把配置元数据提供给 Spring 容器:

    • 基于 XML 的配置文件
    • 基于注解的配置
    • 基于 Java 的配置

    Bean 的作用域

    Bean 的生命周期

    Bean的生命周期可以表达为:Bean的定义——Bean的初始化——Bean的使用——Bean的销毁

    Bean 继承

    bean 定义可以包含很多的配置信息,包括构造函数的参数,属性值,容器的具体信息例如初始化方法,静态工厂方法名,等等。

    子 bean 的定义继承父定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。

    Spring Bean 定义的继承与 Java 类的继承无关,但是继承的概念是一样的。你可以定义一个父 bean 的定义作为模板和其他子 bean 就可以从父 bean 中继承所需的配置。

    <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-3.0.xsd">
    
       <bean id="helloWorld" class="com.xxx.xxx.HelloWorld">
          <property name="message1" value="Hello World!"/>
          <property name="message2" value="Hello Second World!"/>
       </bean>
    
       <bean id="helloWorldC" class="com.xxx.xxx.HelloWorldC" parent="helloWorld">
          <property name="message1" value="Hello!"/>
          <property name="message3" value="Children India!"/>
       </bean>
    
    </beans>

    https://zhuanlan.zhihu.com/p/68818095

  • 相关阅读:
    迈步从头越
    C 语言中用bsearch()实现查找操作
    ASP.NET 远程调试
    JsonHelper
    json数据中包含html代码的解决方法
    js对url的常见操作
    jquery中ajax中get和post的用法
    图片和文件合成为图片的方法(黑客)
    数据库分库知识
    长链接转短链接的方法(百度、新浪、腾讯)
  • 原文地址:https://www.cnblogs.com/feng9exe/p/11214544.html
Copyright © 2011-2022 走看看