码迷,mamicode.com
首页 > 编程语言 > 详细

On the way learning spring 4

时间:2015-10-27 01:48:26      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

Spring-19 Auto-Wiring  by type

在Xml文件中定义bean之间的关系时,有3种方法:

1. inner bean

2. 在bean 的property中定义另外一个bean

3. Spring Autowiring(有很多种类型, 这里 by type)

<bean id="logger" class="com.caveofprogramming.spring.test.Logger"
        autowire="byType">
    </bean>
    <bean id="consolewriter" class="com.caveofprogramming.spring.test.ConsoleWriter">

    </bean>
    <bean id="filewriter" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>

 

Auto-wiring 的两个问题:

1. When project is very big, it hard to know what exactly happen when use Auto-wiring.

2. beans which are wired should exactly matched. It will exist error casue of ambiguity. For example:

<bean id="logger" class="com.caveofprogramming.spring.test.Logger"
        autowire="byType">
    </bean>
    <bean id="consolewriter" class="com.caveofprogramming.spring.test.ConsoleWriter">
    </bean>
    <bean id="consolewriter1" class="com.caveofprogramming.spring.test.ConsoleWriter"> 
    </bean>

 <bean id="filewriter" class="com.caveofprogramming.spring.test.FileWriter"> </bean> </beans>

Interface 的 Implement  也可能出现 ambiguity的问题。

 

package com.caveofprogramming.spring.test;

public class Logger {
    
    private ConsoleWriter consoleWriter;
    private FileWriter fileWriter;
    public void setConsoleWriter(ConsoleWriter consoleWriter) {
        this.consoleWriter = consoleWriter;
    }
    public void setFileWriter(FileWriter fileWriter) {
        this.fileWriter = fileWriter;
    }
    
    public void writeFile(String text){
        fileWriter.write(text);
    }
    
    public void writeConsole(String text){
        consoleWriter.write(text);
    }
    

}

 

 

Spring-20 Autowiring by Name

 

(注意 logger.class与 Spring-19 中的区别 ambiguity)

package com.caveofprogramming.spring.test;

public class Logger {
    
    private LogWriter consoleWriter;
    private LogWriter fileWriter;
    public void setConsoleWriter(LogWriter writer) {
        this.consoleWriter = writer;
    }
    public void setFileWriter(LogWriter fileWriter) {
        this.fileWriter = fileWriter;
    }
    
    public void writeFile(String text){
        fileWriter.write(text);
    }
    
    public void writeConsole(String text){
        consoleWriter.write(text);
    }
    

}

 

    <bean id="logger" class="com.caveofprogramming.spring.test.Logger"
        autowire="byName">
    </bean>
    <bean id="consoleWriter" class="com.caveofprogramming.spring.test.ConsoleWriter">
    </bean>
    <bean id="fileWriter" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>

而且,variables 和 bean中的 id 要 match。

 

Spring-21 Autowiring by Constructor

package com.caveofprogramming.spring.test;

public class Logger {
    
    private LogWriter consoleWriter;
    private LogWriter fileWriter;
    
    public Logger(ConsoleWriter consoleWriter, FileWriter fileWriter){
        this.consoleWriter = consoleWriter;
        this.fileWriter = fileWriter;
    }
    public void setConsoleWriter(LogWriter writer) {
        this.consoleWriter = writer;
    }
    public void setFileWriter(LogWriter fileWriter) {
        this.fileWriter = fileWriter;
    }
    
    public void writeFile(String text){
        fileWriter.write(text);
    }
    
    public void writeConsole(String text){
        consoleWriter.write(text);
    }
    

}
    <bean id="logger" class="com.caveofprogramming.spring.test.Logger"
        autowire="constructor">
    </bean>
    <bean id="consoleWriter" class="com.caveofprogramming.spring.test.ConsoleWriter">
    </bean>
    <bean id="fileWriter" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>

Constructor 是通过Type来match的??

用了autowire=“constructor”后,不能再property??

 

 

Spring-22 Default Autowiring 

解决Defalult Autowiring 中的byType中的ambiguity

package com.caveofprogramming.spring.test;

public class Logger {
    
    private ConsoleWriter consoleWriter;
    private FileWriter fileWriter;
    
    /*    public Logger(ConsoleWriter consoleWriter, FileWriter fileWriter){
            this.consoleWriter = consoleWriter;
            this.fileWriter = fileWriter;
        */
    public void setConsoleWriter(ConsoleWriter writer) {
        this.consoleWriter = writer;
    }
    public void setFileWriter(FileWriter fileWriter) {
        this.fileWriter = fileWriter;
    }
    
    public void writeFile(String text){
        fileWriter.write(text);
    }
    
    public void writeConsole(String text){
        consoleWriter.write(text);
    }
    

}
<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"
    default-autowire="byType"
    default-autowire-candidates="consoleWriter,fileWriter">


    <bean id="logger"
        class="com.caveofprogramming.spring.test.Logger">
    </bean>
    <bean id="consoleWriter" class="com.caveofprogramming.spring.test.ConsoleWriter">
    </bean>
    <bean id="fileWriter" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>
    <bean id="fileWriter1" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>
</beans>

 

 

Spring-22 Removing Autowire Ambiguities

1. autowire-cadidate="false"

2. primary = "true"

<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"
    default-autowire="byType"
    >


    <bean id="logger"
        class="com.caveofprogramming.spring.test.Logger">
    </bean>
    <bean id="consoleWriter" class="com.caveofprogramming.spring.test.ConsoleWriter">
    </bean>
    <bean id="fileWriter" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>
    <bean id="fileWriter1"
        class="com.caveofprogramming.spring.test.FileWriter"
        autowire-candidate="false">
    </bean>
    <bean id="logger"
        class="com.caveofprogramming.spring.test.Logger">
    </bean>
    <bean id="consoleWriter"
        class="com.caveofprogramming.spring.test.ConsoleWriter"
        primary="true">
    </bean>
    <bean id="consoleWriter1" class="com.caveofprogramming.spring.test.ConsoleWriter">
    </bean>
    <bean id="fileWriter" class="com.caveofprogramming.spring.test.FileWriter">
    </bean>
    <bean id="fileWriter1"
        class="com.caveofprogramming.spring.test.FileWriter"
        autowire-candidate="false">
    </bean>

 

On the way learning spring 4

标签:

原文地址:http://www.cnblogs.com/patrickspring/p/4912924.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!