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代码的配置元数据的更多相关文章

  1. Spring IoC — 基于Java类的配置

    普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息. 基于Java类的配置方法和基 ...

  2. Jenkins日常运维笔记-重启数据覆盖问题、迁移、基于java代码发版(maven构建)

    之前在公司机房部署了一套jenkins环境,现需要迁移至IDC机房服务器上,迁移过程中记录了一些细节:1)jenkins默认的主目录放在当前用户家目录路径下的.jenkins目录中.如jenkins使 ...

  3. Spring 基于Java的Bean声明

    Spring 基于Java的Bean声明 使用@Configuration进行设置: Xml: <?xml version="1.0" encoding="UTF- ...

  4. 使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置

    1.使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置 2.上代码:在resources目录下新建:generatorConfiguration.xml文 ...

  5. Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解

    基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置S ...

  6. 基于java代码的springmvc配置

    在我的印象中,开发一个web项目首选当然是springmvc,而配置springmvc无非就是web.xml里配置其核心控制器DispatcherServlet.然后把所有的请求都交给它处理,再配个视 ...

  7. Spring核心技术(十二)——基于Java的容器配置(二)

    使用@Configuration注解 @Configuration注解是一个类级别的注解,表明该对象是用来指定Bean的定义的.@Configuration注解的类通过@Bean注解的方法来声明Bea ...

  8. Spring核心技术(十一)——基于Java的容器配置(一)

    基本概念: @Bean和@Configuration Spring中新的基于Java的配置的核心就是支持@Configuration注解的类以及@Bean注解的方法. @Bean注解用来表示一个方法会 ...

  9. Spring 基于 Java 的配置

    前面已经学习如何使用 XML 配置文件来配置 Spring bean. 基于 Java 的配置可以达到基于XML配置的相同效果. 基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大 ...

随机推荐

  1. Last_IO_Errno: 1062

    主键冲突的错误 1062   模拟错误:   在主库上操作: create table test100(id int not null,name varchar(20),primary key(id) ...

  2. 使用shell脚本实现在liunx上进行svn的上传下载更新功能

    最近有个功能,是需要从在liunx上拉取svn地址,并创建一个新文件进行提交,shell脚本如下 #!/bin/bash echo "Hello World !" myFile=& ...

  3. C语言中%p,%u,%lu都有什么用处

    %p表示输出这个指针, %d表示后面的输出类型为有符号的10进制整形, %u表示无符号10进制整型, %lu表示输出无符号长整型整数 (long unsigned)

  4. Spring3声明式事务处理事务无法回滚rollback分析(annotation与xml配置混用)

    新项目试运行,DBA提示生产数据库一个表的事务20分钟都未提交,分析过程如下: 1.查看日志log文件,最近20分钟是否有error日志: 2.发现某表有insert错误日志,初步判断由该表插入异常, ...

  5. 第8条:覆盖equals时请遵守通用约定

    第8条:覆盖equals时请遵守通用约定 引言:尽管Object是一个具体类,但是设计它主要是为了拓展.它所有的非final方法(equals.hashCode.toString.clone和fina ...

  6. 知识总结和记录——HTML

    文档结构 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="U ...

  7. pdo->prepare 返回false的问题总结

    报错信息: Fatal error: Call to a member function execute() on a non-object 一般是pdo->prepare 返回了false导致 ...

  8. 查询删除的SAP凭证

    标准报表查询:RSSCD100 函数模块:CHANGEDOCUMENT_DISPLAY, Display Change Documents 数据表查询:CDHDR, Change document h ...

  9. ELK+kafka日志处理

    此次使用kafka代替redis,elk集群搭建过程请参考:https://www.cnblogs.com/dmjx/p/9120474.html kafka名词解释: 1.话题(Topic):是特定 ...

  10. Gulp工具

    Gulp是一个基于node开发的构建工具. gulp本身是不进行任何构建任务,是通过gulp的一些列插件完成: gulp-less  编译LESS文件: gulp-autoprefix  添加css私 ...