• 4、深入理解Bean


    本节知识点:
    1. Bean 的自己主动装配(了解)
    2. bean 之间的关系:继承;依赖
    3.Bean 的作用域:能够在 <bean> 元素的 scope 属性里设置 Bean 的作用域
    4.使用外部属性文件
    5. SpEL:Spring 3.x 引入的新特性。用的不多,了解。


    Bean配置总结:

    1. Bean 的自己主动装配(了解):

    0). Bean:

         public class Dao {
        
              private String database;
        
              public void setDatabase(String database) {
                   this.database = database;
              }
              public void save(){
                   System.out.println("save to " + database);
              }
        
         }

         public class Service {

              private Dao dao;
        
              public void setDao(Dao dao) {
                   this.dao = dao;
              }    
              public Dao getDao() {
                   return dao;
              }    
              public void add(){
                   System.out.println("add...");
                   dao.save();
              }
        
         }

         public class Action {

              private Service service = null;
        
              public void setService(Service service) {
                   this.service = service;
              }    
              public Service getService() {
                   return service;
              }    
              public void execute(){
                   System.out.println("execute...");
                   service.add();
              }
        
         }

    1). Spring 能够自己主动的装配 bean 的属性值

    比如,以下的 bean 没有进行属性引用的配置,其装配能够交给 Spring 的 IOC 容器

         <bean id="dao" class="com.atguigu.spring.autowire.Dao">
              <property name="database" value="DB2"/>
         </bean>
         在实际的项目中非常少使用自己主动装配功能,由于和自己主动装配功能所带来的优点比起来。明白清晰的配置文档更有说服力一些

         <bean id="service" class="com.atguigu.spring.autowire.Service" />
         <bean id="action" class="com.atguigu.spring.autowire.Action" />

    2). 在 <bean> 的 autowire 属性里指定自己主动装配的模式

         ①. byType:依据类型自己主动装配
         <bean id="dao" class="com.atguigu.spring.autowire.Dao">
              <property name="database" value="DB2"/>
         </bean>

         <bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byType"/>

         <bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byType"/>

         注意:若 IOC 容器中有多个与目标 Bean 类型一致的 Bean。Spring 将无法判定哪个 Bean 最合适该属性, 所以不能运行自己主动装配.

         <bean id="service2" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
         <bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byName"/>

         <bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byType"/>

         将会抛出:NoUniqueBeanDefinitionException异常。

        

         ②. byName(依据名称自己主动装配):必须将目标 Bean 的名称和属性名设置的全然同样.

         <bean id="service" class="com.atguigu.spring.autowire.Service" autowire="byName"/>
         <bean id="service3" class="com.atguigu.spring.autowire.Service" autowire="byName"/>

         <bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byName"/>

    3). 在实际的项目中非常少使用自己主动装配功能。由于和自己主动装配功能所带来的优点比起来。明白清晰的配置文档更有说服力一些

    -----------------------------------------------------------------------------------------------------------------------------------

    2. Bean 之间的关系:继承。依赖

    1). 继承:并非指 面向对象的继承,而是指 bean 的配置的继承。



    ①. 能够通过 bean 的 parent 属性继承其它 bean 的配置,在当前 bean 中能够覆盖继承的 bean 的属性

         <bean id="car" class="com.atguigu.spring.relation.Car" abstract="true">
              <property name="brand" value="DasAuto"></property>
              <property name="corp" value="ShangHai"></property>
              <property name="maxSpeed" value="200"></property>
              <property name="price" value="100000"></property>
         </bean>

         <!-- 配置一个 car 的 bean, 要求 brand, corp 和 class 和上面的 car 的配置都同样 -->
         <bean id="car2" parent="car">
              <property name="corp" value="YiQi"></property>
              <property name="maxSpeed" value="220"></property>
              <property name="price" value="110000"></property>
         </bean>

    2). 若仅仅想把父 Bean 作为模板, 能够设置 <bean> 的abstract 属性为 true, 这样 Spring 将不会实例化这个 Bean

         <bean id="car" class="com.atguigu.spring.relation.Car" abstract="true">

    3). bean 的依赖关系:Spring 同意用户通过 depends-on 属性设定 Bean 前置依赖的Bean,前置依赖的 Bean 会在本 Bean 实例化之前创建好

         <bean id="action" class="com.atguigu.spring.autowire.Action" autowire="byName" depends-on="service"/>


        
    -----------------------------------------------------------------------------------------------------------------------------------

    3. Bean 的作用域:能够在 <bean> 元素的 scope 属性里设置 Bean 的作用域.

    1). 默认情况下, Spring 仅仅为每一个在 IOC 容器里声明的 Bean 创建唯一一个实例, 整个 IOC 容器范围内都能共享该实例:
         全部兴许的 getBean() 调用和 Bean 引用都将返回这个唯一的 Bean 实例.该作用域被称为 singleton, 它是全部 Bean 的默认作用域.

    2). 若把 scope 的值设置为 prototype  原型,则意为每次 getBean() 方法都会返回一个新的实例。

         <bean id="car2" parent="car" scope="prototype">
              <property name="corp" value="YiQi"></property>
              <property name="maxSpeed" value="220"></property>
              <property name="price" value="110000"></property>
         </bean>

    3). 注意:若 scope 为 singleton, 则 IOC 容器创建时,IOC 容器即创建 Bean 的实例;
                      若 scope 为 prototype。则在 IOC 容器创建时,不再创建 Bean 的实例。而是每次调用 getBean 方法时才创建 Bean 的实例

    4). 内部 Bean 默认情况下也是单例的


    -----------------------------------------------------------------------------------------------------------------------------------
    4.  使用外部属性文件

    1). 必要性:在配置文件中配置 Bean 时, 有时须要在 Bean 的配置里混入系统部署的细节信息(比如: 文件路径, 数据源配置信息等).
    而这些部署细节实际上须要和 Bean 配置相分离

    2). 详细步骤:

         ①. 编写属性文件:在 classpath 下新建 db.properties。并键入例如以下键值对:

         user=root
         password=1230
         jdbcurl=jdbc:mysql:///test
         driverclass=com.mysql.jdbc.Driver
         minsize=10
         maxsize=20
         initsize=10

         ②. 在 bean 的配置文件里导入 properties 文件

              ◆增加 context 命名空间
        
              <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"
                   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        
              ◆使用 <context:property-placeholder location=""/> 导入属性文件
        
              <context:property-placeholder location="classpath:db.properties"/>

         ③. 在 bean 的配置文件里通过 ${} 来使用属性文件里的值:
        
              <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                   <property name="user" value="${user}"></property>
                   <property name="password" value="${password}"></property>
                   <property name="driverClass" value="${driverclass}"></property>
                   <property name="jdbcUrl" value="${jdbcurl}"></property>
        
                   <property name="minPoolSize" value="${minsize}"></property>
                   <property name="maxPoolSize" value="${maxsize}"></property>
                   <property name="initialPoolSize" value="${initsize}"></property>
              </bean>


    -----------------------------------------------------------------------------------------------------------------------------------
    5. SpEL:Spring 3.x 引入的新特性,用的不多,了解。

         1). Spring 表达式语言(简称SpEL):是一个支持执行时查询和操作对象图的强大的表达式语言。
              语法类似于 EL:SpEL 使用 #{…} 作为定界符,全部在大框号中的字符都将被觉得是 SpEL

         2). 样例:

         <bean id="car2" parent="car" scope="prototype">
              <property name="corp" value="YiQi"></property>
              <property name="maxSpeed" value="220"></property>
              <property name="price" value="110000"></property>
         </bean>

         <bean id="dao2" class="com.atguigu.spring.autowire.Dao">
              <property name="database" value="#{car2.toString().toLowerCase()}"></property>
         </bean>

         <bean id="service2" class="com.atguigu.spring.autowire.Service">
              <property name="dao" value="#{dao2}"></property>
         </bean>

    <bean id="action2" class="com.atguigu.spring.autowire.Action">
         <property name="service" value="#{service2}"></property>
    </bean>



  • 相关阅读:
    开发工具 之 PowerDesigner 应用积累
    PowerDesigner 之 PDM建模
    开发工具 之 PowerDesigner
    LCD 和 LED 的区别?
    图像色彩空间YUV和RGB的差别
    ubuntu使用中的一些问题
    FFMPEG-数据结构解释(AVCodecContext,AVStream,AVFormatContext)
    Winform的多线程问题
    C#子线程更新UI控件的方法总结
    malloc(0)的问题
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5093021.html
Copyright © 2020-2023  润新知