@ImportResource in spring imports application xml in configuration file which is using @Configuration. All the beans and other properties defined in application xml can be imported. @ImportResource helps when we are moving our application from old style of defining bean in xml to modern way of defining bean in java file with the help of @Configuration.

In the below example we have one bean defined in xml and one bean defined in java file. We will import the xml with help of @ImportResource and will fetch both beans. 
app-conf.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="subscription" class="com.concretepage.Subscription">
<property name="name" value="Subscription"/>
<property name="type" value="Weekly"/>
</bean>
</beans>

AppConfig.java

package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:/app-conf.xml")
public class AppConfig {
@Bean(name="entitlement")
public Entitlement entitlement(){
Entitlement ent= new Entitlement();
ent.setName("Entitlement");
ent.setTime(20);
return ent;
}
}

Entitlement.java

package com.concretepage;
public class Entitlement {
private String name;
private int time;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
}

Subscription.java

package com.concretepage;
public class Subscription {
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

AppTest.java

package com.concretepage;
import java.sql.SQLException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AppTest {
public static void main(String[] args) throws SQLException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
Entitlement ent= (Entitlement)ctx.getBean("entitlement");
System.out.println(ent.getName());
System.out.println(ent.getTime());
Subscription sub= (Subscription)ctx.getBean("subscription");
System.out.println(sub.getName());
System.out.println(sub.getType());
}
}

Output

Entitlement
20
Subscription
Weekly

参考文献:

【1】http://www.concretepage.com/spring/example_importresource_spring

spring @import和@importResource的更多相关文章

  1. Spring框架中的@Import、@ImportResource注解

    spring@Import @Import注解在4.2之前只支持导入配置类 在4.2,@Import注解支持导入普通的java类,并将其声明成一个bean 使用场景: import注解主要用在基于ja ...

  2. spring源码分析之@ImportSelector、@Import、ImportResource工作原理分析

    1. @importSelector定义: /** * Interface to be implemented by types that determine which @{@link Config ...

  3. @Import与@ImportResource注解的解读

    前言 在使用Spring-Cloud微服务框架的时候,对于@Import和@ImportResource这两个注解想必大家并不陌生.我们会经常用@Import来导入配置类或者导入一个带有@Compon ...

  4. 14 - springboot的@Configuration、@Bean、@Import()、@ImportResource()、@Conditional说明

    1.@Configuration.@Bean.@Import().@ImportResource().@Conditional 分析源码的时候总会见到标题中的这几个注解,因此:弄一篇博客来说明一下吧, ...

  5. Spring Import注解

    今天了解了,Spring @Import的使用 先贴上Spring官方关于Spring @Import注解的文档链接   https://docs.spring.io/spring/docs/3.0. ...

  6. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource

    1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...

  7. spring注解@Import和@ImportResource

    @Import只负责引入javaCOnfig形式定义的Ioc容器配置,等同于<import resource="xxx.xml"/>将一个配置文件导入另一个 @Conf ...

  8. SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource

    Spring Boot提倡基于Java的配置.这两篇博文主要介绍springboot 一些常用的注解介绍 v@value 通过@Value可以将外部的值动态注入到Bean中. 添加applicatio ...

  9. Spring @Import注解源码解析

    简介 Spring 3.0之前,创建Bean可以通过xml配置文件与扫描特定包下面的类来将类注入到Spring IOC容器内.而在Spring 3.0之后提供了JavaConfig的方式,也就是将IO ...

随机推荐

  1. linux 进程通信 管道

    1. 管道概述及相关API应用 1.1 管道相关的关键概念 管道是Linux支持的最初Unix IPC形式之一,具有以下特点: 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管 ...

  2. C++风格的回调对象方法. 采用template实现

    今天看了一篇文章,收藏一下代码.读一读很有激情 #include <iostream> #include <string> #include <vector> us ...

  3. ArcGIS10的附件功能

    转自 积思园 http://blog.csdn.net/linghe301/article/details/6386176 老是忘记怎么使用这个ArcGIS10的附件功能,这次就做个记录吧. 在项目应 ...

  4. Android课程---关于数据存储的学习之总结

  5. .NET支持多平台后的一点拙见

    我们目前对.NET的理解大部分可以归纳为:起初它是Java平台(注意是平台,不要跟Java语言搞混淆)的一个克隆品,后来慢慢演变,有了自己的特性.由于Java平台最显著的特点就是“平台独立性”(或者说 ...

  6. Meteor + node-imap(nodejs) + mailparser(nodejs) 实现完整收发邮件

    版本信息: Meteor:windows MIS安装  0.6.4 node-imap:npm指定的0.8.0版,不是默认的0.7.x版. mailparser:npm安装0.3.6 以下是记录踩到的 ...

  7. 先有 Mac 还是先有银元?

    自从开设了这个公众号以后,收到最多的问题是:听了 Mac 君的介绍,我也觉得 Mac 很好,可是穷屌丝一枚,真买不起,怎么破?前几天我在微博上说,读了我的书,就有 Mac 了.居然有读者来问:这是真的 ...

  8. C语言 · 出现次数最多的数

    问题描述 编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会超过20.然后程序将对这个数组进行统计,把出现次数最多的那个数组元素值打印出来.如果有两 ...

  9. SharedPreferences.Editor 的apply()与commit()方法的区别

    commit()的文档 官方文档如下: Commit your preferences changes back from this Editor to the SharedPreferences o ...

  10. Swift语言快速入门

    Swift语言快速入门(首部同步新版官方API文档和语法的Swift图书,确保代码可编译,作者专家在线答疑,图书勘误实时跟进) 极客学院 编著   ISBN 978-7-121-24328-8 201 ...