简介

这个注解很简单,就是导入spring的xml配置文件

直接来看spring官方文档:

In applications where @Configuration classes are the primary mechanism for configuring the container, it will still likely be necessary to use at least some XML. In these scenarios, simply use @ImportResource and define only as much XML as is needed. Doing so achieves a "Java-centric" approach to configuring the container and keeps XML to a bare minimum.

当我们使用java配置类的时候,比如springboot工程,就推荐使用java配置类,理论上我们可以完全消除xml配置文件,但是有时候我们需要导入spring的xml配置文件,就需要使用这个注解

举例:

首先是我们的java配置类

@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig { @Value("${jdbc.url}")
private String url; @Value("${jdbc.username}")
private String username; @Value("${jdbc.password}")
private String password; @Bean
public DataSource dataSource() {
return new DriverManagerDataSource(url, username, password);
}
}
properties-config.xml
<beans>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
</beans>

jdbc.properties

jdbc.properties
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb
jdbc.username=sa
jdbc.password=

运行测试方法:

public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
TransferService transferService = ctx.getBean(TransferService.class);
// ...结果略
}

说明

以上是spring官方文档对于该注解的说明和示例,比较简单,不赘述。

最新文章

  1. PHP笔记(HTML篇)
  2. arcgis engine 调用arcgis server服务
  3. BZOJ1767 : [Ceoi2009]harbingers
  4. 弄清const与指针、引用之间的关系
  5. 第一个PHP程序
  6. 特征值分解,奇异值分解(SVD)
  7. 原生js 学习之array 数组
  8. MySQL 指定数据库字符集的 3 种方法。
  9. Android应用程序组成部分
  10. WebApi Ajax 跨域请求解决方法(CORS实现)
  11. dcoker实战,使用docker部署NodeJs应用
  12. python爬微信公众号前10篇历史文章(2)-拼接URL&amp;发送http请求
  13. [20190423]简单测试latch nowilling等待模式.txt
  14. Winform 加载datagridview
  15. EDK II之Secure Boot简述
  16. es集群搭建
  17. 设置PHPStorm 注释
  18. 简单记录下3PC
  19. switch的用法
  20. 用Fiddler可以设置浏览器的UA 和 手动 --Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!

热门文章

  1. [转载]Linux进程调度原理
  2. Collections.synchronizedList与CopyOnWriteArrayList比较
  3. Boosting and AdaBoost
  4. python 字符串方法整理
  5. learning java 重定向标准输入输出
  6. CLR如何将SEH异常映射到托管异常类型
  7. 【批处理】choice命令,call 命令,start 命令,rem
  8. fio 文件系统io 性能测试安装使用
  9. ipcm
  10. PHP-FPM config 文件生产环境