spring还支持基于java代码的配置元数据。不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下。
spring基于java代码的配置元数据,可以通过@Configuration注解把一个声明为配置类;通过@Bean注解把一个新
创建的类交由spring容器来管理。在这种配置方式下,我们可以手动装配bean,也可以自动装配bean.我感觉在这种
方式下使用手动装配非常不爽,尤其是有多个配置类的时候。
下面看个例子:
1.新建包com.tutorialspoint.javacode,并在包中新建TextEditor.java、SpellChecker.java、HighLighter.java
//TextEditor.java
packagecom.tutorialspoint.javacode;importjavax.annotation.Resource;public classTextEditor {privateSpellChecker spellChecker;privateHighLighter highLighter;
@Resourcepublic voidsetHighLighter(HighLighter highLighter) {this.highLighter =highLighter;
}
@Resourcepublic voidsetSpellChecker(SpellChecker spellChecker) {this.spellChecker =spellChecker;
}publicTextEditor(){}publicTextEditor(SpellChecker spellChecker){this.spellChecker=spellChecker;
}publicTextEditor(HighLighter highLighter){this.highLighter=highLighter;
}public voidinit(){
System.out.println("init method invoked...");
}public voiddestroy(){
System.out.println("destroy method invoked...");
}public voidprint(){
System.out.println("spellChecker: "+spellChecker);
System.out.println("highLighter: "+highLighter);
}
}//SpellChecker.java
packagecom.tutorialspoint.javacode;public classSpellChecker {public voidcheckSpell(){
System.out.println("checking...");
}
}//HighLighter.java
packagecom.tutorialspoint.javacode;public classHighLighter {public voidhighlight(){
System.out.println("highlighting...");
}
}
2.在包com.tutorialspoint.javacode中新建如下三个配置类:
//AppConfig.java
packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Import;/***使用 @Configuration注解表明一个类属于配置类
*可以通过@Import注解引入其他配置类。
**/@Configuration
@Import(value={SpellCheckerConfig.class,HighLighterConfig.class})public classAppConfig {/*** 可以通过使用@Bean注解的name属性指定该bean在spring容器中的名字
* 使用initMethod属性指定该bean的初始化方法
* 使用destroyMethod属性指定bean的销毁方法*/@Bean(name="textEditor",initMethod="init",destroyMethod="destroy")publicTextEditor textEditor(){return newTextEditor();
}
}//HighLighterConfig.java
packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;
@Configurationpublic classHighLighterConfig {
@Bean(name="highLighter")publicHighLighter highLighter(){return newHighLighter();
}
}//SpellCheckerConfig.java
packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;
@Configurationpublic classSpellCheckerConfig {
@Bean(name="spellChecker")publicSpellChecker spellChecker(){return newSpellChecker();
}
}
3.在包com.tutorialspoint.javacode中新建MainApp.java.内容如下:
packagecom.tutorialspoint.javacode;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;importorg.springframework.context.support.AbstractApplicationContext;public classMainApp {public static voidmain(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代码的配置元数据无法支持构造器参数方式的自动依赖注入,必须手动装配构造器参数。