[译]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 的情况下编写大 ...
随机推荐
- 问答 请问使用OK("raw:jpg")能返回多张图片吗
请问使用OK("raw:jpg")能返回多张图片吗 发布于 28天前 作者 qq_3aeeb0ad 78 次浏览 复制 上一个帖子 下一个帖子 标签: 无 @At( ...
- hdu-3584 Cube---三维树状数组+区域更新单点查询
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3584 题目大意: 给定一个N*N*N多维数据集A,其元素是0或是1.A[i,j,k]表示集合中第 i ...
- 判断团队适不适合使用node
1.要不要用 2.历史包袱 3.跟进升级 看完scott创业公司使用node,对于一个团队要不要使用node,第一个就是如果承接的项目有很多历史迭代,线上也在稳定的抛,不要轻易的替换,比如很多老代码, ...
- PHP编译安装时常见错误及解决办法,大全
1. configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution ...
- python 3+djanjo 2.0.7简单学习(二)--创建数据库和模型
我们紧接上次,这里将建立数据库,创建第一个模型提示:这里我们不需要去一直启动,django会在我们ctrl+s的时候自动刷新并启动服务,很方便吧 1.数据库配置 现在,打开 vote_mysite/ ...
- 洛谷 P3372 线段树1
这是一道模板题 线段树介绍https://www.cnblogs.com/nvwang123/p/10420832.html #include<bits/stdc++.h> using n ...
- github上更新fork项目
转载:https://blog.csdn.net/qq1332479771/article/details/56087333 ps:需要用GitHub所指定的chrome或者firefox浏览器,其它 ...
- [JZOJ] 5905. 黑暗之魂(darksoul)
基环树直径裸题 分别在子树做DP,环上做DP,环上可以用单调队列优化到\(O(n)\) 写起来很麻烦 #include<algorithm> #include<iostream> ...
- 【杂题总汇】UVa-1627 Team them up!
[UVa-1627] Team them up! 借鉴了一下hahalidaxin的博客……了解了思路,但是莫名Wa了:最后再找了一篇dwtfukgv的博客才做出来
- python基础数据类型之列表,元组操作
一.列表的索引和切片1.列表的索引列表和字符串一样样拥有索引 lst = ["a","b","c"] print(lst[0]) # 获取第 ...