Spring继承与依赖注入-three

Spring Bean 定义继承
  • Bean可以通过设置配置文件来定义继承关系
  • 子 bean 的定义继承父定义的配置数据。子定义可以根据需要重写一些值,或者添加其他值。tip:Spring Bean 定义的继承与 Java 类的继承无关,但是继承的概念是一样的。
  • 当你使用基于 XML 的配置元数据时,通过使用父属性,指定父 bean 作为该属性的值来表明子 bean 的定义。

通过XML配置文件实现继承关系实例


      
      
   

   
      
      
   

tip:在定义Student类的时候我们不再需要继承Person类

Bean 定义模板

      
      
   

   
      
      
   

person 自身不能被实例化,因为它是不完整的(没有指定class属性),而且它也被明确地标记为抽象的。当一个定义是抽象的,它仅仅作为一个纯粹的模板 bean 定义来使用的,充当子定义的父定义使用。

Spring 依赖注入

当编写一个复杂的 Java 应用程序时,应用程序的 java 有多个对象,应用程序类应该尽可能独立于其他 Java 类来增加这些类重用的可能性,依赖注入DI
(或有时称为布线)有助于把这些类粘合在一起,同时保持他们独立

  • 构造函数注入
public class Person{
   private Speak speak;
   public Person(Speak speak) {
      this.speak= speak;
   }
}

依赖关系通过类构造函数被注入到 Person类中。

  • setter方法注入
public class Person{
   private Speak speak;
   public void setSpeak(Speak speak){
       this.speak=speak;
   }
}

依赖关系通过类构造函数被注入到 Person类中。控制流通过依赖注入(DI)已经“反转”,因为你已经有效地委托依赖关系到一些外部系统。

  • XML配置文件注入
 
      




Spring 基于设值函数的依赖注入

当容器调用一个无参的构造函数或一个无参的静态 factory 方法来初始化你的 bean 后,通过容器在你的 bean 上调用设值函数,基于设值函数的 DI 就完成了。

 
      




  • Tip:此方法和构造函数注入唯一的区别就是在基于构造函数注入中,我们使用的是标签中的元素,而在基于设值函数的注入中,我们使用的是标签。

  • tip:如果你要把一个引用传递给一个对象,那么你需要使用 标签的 ref 属性,而如果你要直接传递一个值,那么你应该使用 value 属性。

p-namespace

      
      

//可以通过如下方式简化表示


注入内部bean

inner bean是在bean类中添加的内部类(java内部类)


        
            
        

tip:``

Spring注入集合
Spring集合类
Map
Properties
Set
List

Spring提供了四种集合类

Spring集合类
Map
Properties
Set
List

以下是Collections类和springbean.xml的配置代码

package com.sunxiaohang;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Collections {
    private Set collectionSet;
    private Map collectionMap;
    private Properties collectionProperties;
    private List collectionList;

    public Collections() {
    }

    public Set getCollectionSet() {
        System.out.println("Set Element:"+collectionSet);
        return collectionSet;
    }

    public void setCollectionSet(Set collectionSet) {
        this.collectionSet = collectionSet;
    }

    public Map getCollectionMap() {
        System.out.println("Map Element:"+collectionMap);
        return collectionMap;
    }

    public void setCollectionMap(Map collectionMap) {
        this.collectionMap = collectionMap;
    }

    public Properties getCollectionProperties() {
        System.out.println("Properties Element:"+collectionProperties);
        return collectionProperties;
    }

    public void setCollectionProperties(Properties collectionProperties) {
        this.collectionProperties = collectionProperties;
    }

    public List getCollectionList() {
        System.out.println("list Element:"+collectionList);
        return collectionList;
    }

    public void setCollectionList(List collectionList) {
        this.collectionList = collectionList;
    }
}


    
        
            张三
            李四
            王五
            马六
        
    
    
        
            北京
            天津
            上海
            广州
        
    
    
        
            
            
            
            
        
    
    
        
            姓名
            性别
            年龄
            出生日期
        
    

我的文章列表

你可能感兴趣的:(Spring继承与依赖注入-three)