[译]17-spring基于java代码的配置元数据
2015-05-08 11:44
597 查看
spring还支持基于java代码的配置元数据。不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下。
spring基于java代码的配置元数据,可以通过@Configuration注解把一个声明为配置类;通过@Bean注解把一个新
创建的类交由spring容器来管理。在这种配置方式下,我们可以手动装配bean,也可以自动装配bean.我感觉在这种
方式下使用手动装配非常不爽,尤其是有多个配置类的时候。
下面看个例子:
1.新建包com.tutorialspoint.javacode,并在包中新建TextEditor.java、SpellChecker.java、HighLighter.java
2.在包com.tutorialspoint.javacode中新建如下三个配置类:
3.在包com.tutorialspoint.javacode中新建MainApp.java.内容如下:
4.运行程序,检查结果:
但是基于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代码的配置元数据无法支持构造器参数方式的自动依赖注入,必须手动装配构造器参数。
相关文章推荐
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(1) - 数据源与事务管理
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(2) - 静态资源、视图和消息器
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置
- spring基于java配置@Configuration和@Bean用法【代码示例】
- 基于java代码的Spring-mvc框架配置
- 基于纯Java代码的Spring容器和Web容器零配置的思考和实现(3) - 使用配置
- Spring学习历程 --- 基于 Java 类的配置
- spring AOP 之 xml 配置实现(附 Java 代码实例)
- spring AOP 之 注解 配置实现(附 Java 代码实例)
- Spring 基于XML配置 基于注解配置 基于JAVA类配置比较以及适用场景
- spring基于java注解的配置
- Spring web基于javaconfig类配置 的web.XML配置
- Spring中基于Java的配置@Configuration和@Bean用法
- 基于 java 配置Spring
- Spring基于Java注解的配置
- 润乾报表在websphere8部署配置数据源报错 java.sql.SQLException: 调用中无效的参数DSRA0010E: SQL 状态 = null,错误代码 = 17,433 而失败
- Spring中基于Java的配置@Configuration和@Bean用法
- Spring中基于Java的配置@Configuration和@Bean用法
- Spring中基于Java的配置@Configuration和@Bean用法 (转)
- redis在spring中的配置及java代码实现