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 ...
随机推荐
- Ant环境变量配置
Ant环境变量配置 1.新建系统变量ANT_HOME 变量名: ANT_HOME 变量值: D:\biancheng\apache-ant-1.7.1 2.修改PATH 变量值最后面 ...
- https://oj.leetcode.com/problems/majority-element/
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 谷歌浏览器允许ajax跨域以非安全模式打开
最近使用ajax的时候,因为是在本地测试调用 后台时一直会报错. 解决方案:用谷歌浏览器 以非安全的模式打开 在cmd命令行中 cd 到谷歌的安装目录下 (右键 属性 复制路径) 然后在 运行如下命令 ...
- chrome浏览器js 导出excel
<table id="table"> <tr> <th>ID</th> <th>姓名</th> <th ...
- Visual Studio 2015 CTP6 发布
微软发布ASP.NET 5 支持在Windows.Mac和Linux上构建程序,Visual Studio 2015 CTP6(社区预览版)现已发布了.感兴趣的朋友们可以登录官网下载[http://w ...
- Lesson 1 A private conversation
Text Last week I went to the theatre. I had a very good seat. The play was very intersting. I did no ...
- 每周一书-《鸟哥的Linux私房菜》获奖公布
<鸟哥的Linux私房菜>一书的赠书活动时间为2016年10月19日到10月31日, 也就是今天结束. 首先要感谢QQ号为:1084830483(路在远方),来自哈尔滨工程大学的同学赠送给 ...
- .NET中使用APlayer组件自制播放器
目录 说明 APlayer介绍 APlayer具备功能 APlayer使用 自制播放器Demo 未完成工作 源码下载 说明 由于需求原因,需要在项目中(桌面程序)集成一个在线播放视频的功能.大概要具备 ...
- 让浏览器不再显示 https 页面中的 http 请求警报
HTTPS 是 HTTP over Secure Socket Layer,以安全为目标的 HTTP 通道,所以在 HTTPS 承载的页面上不允许出现 http 请求,一旦出现就是提示或报错: Mix ...
- C#设计模式之观察者
Iron之观察者 引言 上一篇说的职责链模式,很有意思的一个模式,今天这个模式也是很有意思的一个模式,还是不啰嗦了直接进入主题吧. 场景介绍:在上一遍中说到用到部件检测,很巧妙的让调用者和处理者解耦了 ...