spring @import和@importResource
@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的更多相关文章
- Spring框架中的@Import、@ImportResource注解
spring@Import @Import注解在4.2之前只支持导入配置类 在4.2,@Import注解支持导入普通的java类,并将其声明成一个bean 使用场景: import注解主要用在基于ja ...
- spring源码分析之@ImportSelector、@Import、ImportResource工作原理分析
1. @importSelector定义: /** * Interface to be implemented by types that determine which @{@link Config ...
- @Import与@ImportResource注解的解读
前言 在使用Spring-Cloud微服务框架的时候,对于@Import和@ImportResource这两个注解想必大家并不陌生.我们会经常用@Import来导入配置类或者导入一个带有@Compon ...
- 14 - springboot的@Configuration、@Bean、@Import()、@ImportResource()、@Conditional说明
1.@Configuration.@Bean.@Import().@ImportResource().@Conditional 分析源码的时候总会见到标题中的这几个注解,因此:弄一篇博客来说明一下吧, ...
- Spring Import注解
今天了解了,Spring @Import的使用 先贴上Spring官方关于Spring @Import注解的文档链接 https://docs.spring.io/spring/docs/3.0. ...
- SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
1. package soundsystem; import org.springframework.beans.factory.annotation.Autowired; public class ...
- spring注解@Import和@ImportResource
@Import只负责引入javaCOnfig形式定义的Ioc容器配置,等同于<import resource="xxx.xml"/>将一个配置文件导入另一个 @Conf ...
- SpringBoot入门教程(十八)@value、@Import、@ImportResource、@PropertySource
Spring Boot提倡基于Java的配置.这两篇博文主要介绍springboot 一些常用的注解介绍 v@value 通过@Value可以将外部的值动态注入到Bean中. 添加applicatio ...
- Spring @Import注解源码解析
简介 Spring 3.0之前,创建Bean可以通过xml配置文件与扫描特定包下面的类来将类注入到Spring IOC容器内.而在Spring 3.0之后提供了JavaConfig的方式,也就是将IO ...
随机推荐
- dictionary 应用(绑定dgv)
dictionary的用法://初始化添加所有车位进这队列 Utility.Effectlist.Add(); //每触发一次,用这个增加数量 Utility.Effectlist["Car ...
- [原创]MySQL innodb_rollback_on_timeout参数对锁的影响
环境:Server version: 5.6.21-log MySQL Community Server (GPL) 前提提要: innodb_rollback_on_timeout是 ...
- MySQL操作使用
这只是一些简单的数据库命令,作为新手记录一下,以供后面查询使用. 查询服务器版本号和当前日期: select version(), current_date; 一个命令通常用一个SQL语句组成,后面跟 ...
- 字节流与数据类型的相互转换---使用struct模块
字节流与数据类型的相互转换---使用struct模块 http://blog.csdn.net/Sunboy_2050/article/details/5974029 Python是一门非常简洁的语言 ...
- 【转】我的技术学习方法 — Anytao
原文作者:anytao—王涛 他的著作:<你必须知道的.Net> 关于这个问题,也有不少刚刚入行的朋友向我问起.我想可能一千个人就有一千个答案,我不能保证自己的想法适合于所有的人,但是这确 ...
- linux 使用fdisk分区扩容
标签:fdisk分区 概述 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,在这个时候我们就需要增加磁盘空间,本章主要介绍如何使用fdisk分区工具创建磁盘分区和挂载分区,介绍两种情 ...
- SQL Server对比两字段的相似度(函数算法)
相似度函数 概述 比较两个字段的相似度 最近有人问到关于两个字段求相似度的函数,所以就写了一篇关于相似度的函数,分别是“简单的模糊匹配”,“顺序匹配”,“一对一位置匹配”.在平时的这种函数 ...
- salesforce 零基础学习(五十一)使用 Salesforce.com SOAP API 实现用户登录以及简单的增删改查(JAVA访问salesforce)
此篇请参看:https://resources.docs.salesforce.com/202/latest/en-us/sfdc/pdf/salesforce_developer_environme ...
- java反射 之 反射基础
一.反射 反射:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...
- .net 网络编程
1.首先说下计算机网络中的TCP/IP参考模型 TCP/IP把网络分为5层,每一层负责完成不同的功能 1)应用层:传输报文,提供各种网络应用,有FTP.SMTP.HTTP等协议 2)运输层:传输报文段 ...