[译]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代码的配置元数据无法支持构造器参数方式的自动依赖注入,必须手动装配构造器参数。
[译]17-spring基于java代码的配置元数据的更多相关文章
- Spring IoC — 基于Java类的配置
普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息. 基于Java类的配置方法和基 ...
- Jenkins日常运维笔记-重启数据覆盖问题、迁移、基于java代码发版(maven构建)
之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中.如jenkins使 ...
- Spring 基于Java的Bean声明
Spring 基于Java的Bean声明 使用@Configuration进行设置: Xml: <?xml version="1.0" encoding="UTF- ...
- 使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置
1.使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置 2.上代码:在resources目录下新建:generatorConfiguration.xml文 ...
- Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解
基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...
- 基于java代码的springmvc配置
在我的印象中,开发一个web项目首选当然是springmvc,而配置springmvc无非就是web.xml里配置其核心控制器DispatcherServlet.然后把所有的请求都交给它处理,再配个视 ...
- Spring核心技术(十二)——基于Java的容器配置(二)
使用@Configuration注解 @Configuration注解是一个类级别的注解,表明该对象是用来指定Bean的定义的.@Configuration注解的类通过@Bean注解的方法来声明Bea ...
- Spring核心技术(十一)——基于Java的容器配置(一)
基本概念: @Bean和@Configuration Spring中新的基于Java的配置的核心就是支持@Configuration注解的类以及@Bean注解的方法. @Bean注解用来表示一个方法会 ...
- Spring 基于 Java 的配置
前面已经学习如何使用 XML 配置文件来配置 Spring bean. 基于 Java 的配置可以达到基于XML配置的相同效果. 基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大 ...
随机推荐
- hdu-1892 See you~---二维树状数组运用
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 题目大意: 题目大意:有很多方格,每个方格对应的坐标为(I,J),刚开始时每个格子里有1本书, ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) H】命命命运(概率DP)
点此看题面 大致题意: 有\(6\)个人玩大富翁,共有\(n\)块地,进行\(500\)轮,已知每个人掷骰子掷出\(1\sim6\)的概率.当某人到达一块未被占领的地时,他可以占领它.求最后每个人占有 ...
- 高精度水题(POJ2109)
题目链接:http://poj.org/problem?id=2109 double 可以虽然可以表示10^-307~~~10^208,但是精确度只有16位,这个题有bug. #include < ...
- E. New Reform_贪心,深搜,广搜。
E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Heterogeneity Activity Recognition Data Set类别
Heterogeneity Activity Recognition Data Set:https://archive.ics.uci.edu/ml/datasets/Heterogeneity+Ac ...
- .svn文件被删除的解决办法
不小心把文件夹下的.svn给删除了,svn提交时会报如下错误: 包含工作副本管理数据的目录“/home/usa/svn/aispeech/air201102/branches/tools/res/di ...
- elsevier期刊要求翻译
百度文库 http://wenku.baidu.com/view/e20a27e84afe04a1b071de4e.html 官网文档 http://www.elsevier.com/journals ...
- 【主席树上二分】bzoj5361: [Lydsy1805月赛]对称数
随机化选讲例题 题目大意 小 Q 认为,偶数具有对称美,而奇数则没有.给定一棵 n 个点的树,任意两点之间有且仅有一条直接或间接路径.这些点编号依次为 1 到 n,其中编号为 i 的点上有一个正整数 ...
- mysql指定id默认第一
有个需求 家庭创建人要默认排第一,刚开始用加入家庭的时间排序可以 好简单, 后来加了一个需求 家庭创建人可以转移,结果按时间排序就不行了,又不想去写循环重新排序 就各种百度, 等于就是指定ID排最后 ...
- GNU汇编 存储器访问指令
.text .global _start _start: mov r0,#0xff str r0,[r1] ldr r2,[r1]