Spring中使用byType實現Beans自動裝配


以下內容引用自http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byType.html

此模式通過屬性類型來指定自動裝配。Spring容器查看XML配置文件中將autowire屬性設置為byTypebean。然后,如果它的類型與配置文件中的一個bean名稱匹配,它將嘗試匹配和連接一個屬性。如果找到匹配項,它將注入這些bean。否則,bean將不會被連線。

例如,如果在配置文件中將bean定義設置為autowire="byType",並且它包含spellChecker類型的spellChecker屬性,則Spring會查找名為spellChecker的bean定義,並使用它來設置該屬性。當然,還可以使用<property>標簽連接剩余的屬性。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jsoft.testspring</groupId>
  <artifactId>testautowiringbytype</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testautowiringbytype</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency>
    
  </dependencies>
</project>

SpellChecker.java:

package com.jsoft.testspring.testautowiringbytype;

public class SpellChecker {
    public SpellChecker(){
        System.out.println("SpellChecker無參數構造函數初始化");
    }
    
    public void checkSpelling(){
        System.out.println("SpellChecker檢查方法");
    }
}

TextEditor.java:

package com.jsoft.testspring.testautowiringbytype;

public class TextEditor {
    private SpellChecker spellChecker;
    private String name;
    
    public void setSpellChecker(SpellChecker spellChecker){
        System.out.println("TextEditor通過setter初始化");
        this.spellChecker = spellChecker;
    }
    
    public void spellCheck() {
        this.spellChecker.checkSpelling();
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public void getName(){
        System.out.println(this.name);
    }
    
}

beans.xml:

<?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.xsd">
    
    <bean id="spellChecker" class="com.jsoft.testspring.testautowiringbytype.SpellChecker"></bean>
    
    <bean id="textEditor" class="com.jsoft.testspring.testautowiringbytype.TextEditor" autowire="byType">
        <property name="Name" value="Hello World!"></property>
    </bean>
   
</beans>

修改之前的beans.xml:

<?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.xsd">
    
    <bean id="spellChecker" class="com.jsoft.testspring.testautowiringbytype.SpellChecker"></bean>
    
    <bean id="textEditor" class="com.jsoft.testspring.testautowiringbytype.TextEditor">
        <property name="SpellChecker" ref="spellChecker"></property>
        <property name="Name" value="Hello World!"></property>
    </bean>
   
</beans>

運行結果:

 

測試工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test11/testautowiringbytype


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM