[译]17-spring基于java代码的配置元数据

spring还支持基于java代码的配置元数据。不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下。

spring基于java代码的配置元数据,可以通过@Configuration注解把一个声明为配置类;通过@Bean注解把一个新

创建的类交由spring容器来管理。在这种配置方式下,我们可以手动装配bean,也可以自动装配bean.我感觉在这种

方式下使用手动装配非常不爽,尤其是有多个配置类的时候。

 

下面看个例子:

1.新建包com.tutorialspoint.javacode,并在包中新建TextEditor.java、SpellChecker.java、HighLighter.java

//TextEditor.java

package 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.java

package com.tutorialspoint.javacode;

public class SpellChecker {

    public void checkSpell(){
        System.out.println("checking...");
    }
}

//HighLighter.java

package com.tutorialspoint.javacode;

public class HighLighter {

    public void highlight(){
        System.out.println("highlighting...");
    }
}

2.在包com.tutorialspoint.javacode中新建如下三个配置类:

//AppConfig.java

package 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.java

package com.tutorialspoint.javacode;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HighLighterConfig {

    @Bean(name="highLighter")
    public HighLighter highLighter(){
        return new HighLighter();
    }
}


//SpellCheckerConfig.java

package com.tutorialspoint.javacode;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public 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代码的配置元数据无法支持构造器参数方式的自动依赖注入,必须手动装配构造器参数。

posted @ 2015-05-08 11:44  sysman  阅读(955)  评论(0编辑  收藏  举报