[譯]17-spring基於java代碼的配置中繼資料

來源:互聯網
上載者:User

標籤:

spring還支援基於java代碼的配置中繼資料。不過這種方式不太常用,但是還有一些人使用.所以還是很有必要介紹一下。

spring基於java代碼的配置中繼資料,可以通過@Configuration註解把一個聲明為配置類;通過@Bean註解把一個新

建立的類交由spring容器來管理。在這種配置方式下,我們可以手動裝配bean,也可以自動裝配bean.我感覺在這種

方式下使用手動裝配非常不爽,尤其是有多個配置類的時候。

 

下面看個例子:

1.建立包com.tutorialspoint.javacode,並在包中建立TextEditor.java、SpellChecker.java、HighLighter.java

//TextEditor.javapackage com.tutorialspoint.javacode;import javax.annotation.Resource;public class TextEditor {    private SpellChecker spellChecker;    private HighLighter highLighter;            @Resource    public void setHighLighter(HighLighter highLighter) {        this.highLighter = highLighter;    }    @Resource    public void setSpellChecker(SpellChecker spellChecker) {        this.spellChecker = spellChecker;    }        public TextEditor(){}    public TextEditor(SpellChecker spellChecker){        this.spellChecker=spellChecker;    }    public TextEditor(HighLighter highLighter){        this.highLighter=highLighter;    }        public void init(){        System.out.println("init method invoked...");    }    public void destroy(){        System.out.println("destroy method invoked...");    }        public void print(){        System.out.println("spellChecker: "+spellChecker);        System.out.println("highLighter: "+highLighter);    }    }//SpellChecker.javapackage com.tutorialspoint.javacode;public class SpellChecker {    public void checkSpell(){        System.out.println("checking...");    }}//HighLighter.javapackage com.tutorialspoint.javacode;public class HighLighter {    public void highlight(){        System.out.println("highlighting...");    }}

2.在包com.tutorialspoint.javacode中建立如下三個配置類:

//AppConfig.javapackage com.tutorialspoint.javacode;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;/** *使用 @Configuration註解表明一個類屬於配置類 *可以通過@Import註解引入其他配置類。 * */@Configuration@Import(value={SpellCheckerConfig.class,HighLighterConfig.class})public class AppConfig {    /**     * 可以通過使用@Bean註解的name屬性指定該bean在spring容器中的名字     * 使用initMethod屬性指定該bean的初始化方法     * 使用destroyMethod屬性指定bean的銷毀方法     */    @Bean(name="textEditor",initMethod="init",destroyMethod="destroy")    public TextEditor textEditor(){        return new TextEditor();    }    }//HighLighterConfig.javapackage com.tutorialspoint.javacode;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class HighLighterConfig {    @Bean(name="highLighter")    public HighLighter highLighter(){        return new HighLighter();    }}//SpellCheckerConfig.javapackage com.tutorialspoint.javacode;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class SpellCheckerConfig {    @Bean(name="spellChecker")    public SpellChecker spellChecker(){        return new SpellChecker();    }        }

 

3.在包com.tutorialspoint.javacode中建立MainApp.java.內容如下:

package com.tutorialspoint.javacode;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.AbstractApplicationContext;public class MainApp {    public static void main(String[] args) {                //基於java代碼的容器的實作類別要使用AnnotationConfigApplicationContext        AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);                TextEditor te = (TextEditor) ctx.getBean(TextEditor.class);                te.print();                //為了使bean的銷毀方法起作用,註冊JVM的退出事件        ctx.registerShutdownHook();    }}

 

4.運行程式,檢查結果:

 

但是基於java代碼的配置中繼資料無法支援構造器參數方式的自動依賴注入,必須手動裝配構造器參數。

[譯]17-spring基於java代碼的配置中繼資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.