[译]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 的情况下编写大 ...
随机推荐
- openwrt定制管理
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qianguozheng/article/details/24673097 近期这个比較火,可是改了东 ...
- nginx里面的rewrite配置
哎,我需要静静,刚刚在去怎么优化dom层级,发现更新完代码,层级又蹭蹭蹭的往上涨,顿时没脾气了,还是把昨天的nginx配置总结下,增加点动力,昨天前天两天都在搞这个问题,也是搞的没脾气,网上查了很多资 ...
- win10 下的python虚拟环境安装使用(使用powershell)
安装virtualenv 若要使用python虚拟环境进行开发,首先需要安装virtualenv.命令:pip install virtualenv 我已经装过了
- P1151 子数整数
题目描述 对于一个五位数a_1a_2a_3a_4a_5a1a2a3a4a5,可将其拆分为三个子数: sub_1=a_1a_2a_3sub1=a1a2a3 sub_2=a_2a_3a_ ...
- 2018.7.23 oracle中的CLOB数据类型
Oarcle中的LOB类型 1.在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了.因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这 ...
- EF 连接数据库 Mysql (database first ) 一个表对应一个模型
准备工作 1.下载vs2015 2.下载mysql2017 3.安装 1.创建数据库 2. 将数据库映射成模型 3创建aspx 文件. 写下窗体内容的代码 hello_worldEntities en ...
- js中的变量提升(Hoisting)
<script> function test(){ console.log(a); console.log(foo()); var a=1; function foo(){ return ...
- print_Matrix(Python实现)
num = int(input("Please input a number:")) #矩阵最外层的值 n = num*2 Matrix = [([0] * n)for i in ...
- javascript入门笔记5-事件
1.继续循环continue; continue的作用是仅仅跳过本次循环,而整个循环体继续执行. 语句结构: for(初始条件;判断条件;循环后条件值更新) { if(特殊情况) { continue ...
- Linux tmpwatch命令
Linux tmpwatch命令 作为系统管理员,很多时候需要定期清理一定规则的文件,比如过期的日志,过期的归档,已备份的文件等等. 如果使用一定的匹配规则,找出这些文件,然后再传递给rm命令,其实是 ...