博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring @import和@importResource
阅读量:7063 次
发布时间:2019-06-28

本文共 2645 字,大约阅读时间需要 8 分钟。

@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

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

Entitlement20Subscription Weekly

 

参考文献:

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

转载地址:http://xrnll.baihongyu.com/

你可能感兴趣的文章
Linux网络相关、firewalld和netfilter
查看>>
linux基础(day30)
查看>>
四周第五次课(11月10日) 6.5 zip压缩工具 6.6 tar打包 6.7 打包并压缩
查看>>
Redis——HyperLogLog
查看>>
科技兴国园区兴城——2019国际高科技产业园区博览会在深盛装开幕
查看>>
为什么你不能在 MySQL 3.x 版本上安装 Joomla 1.5.23
查看>>
Exchange 2013公网证书配置
查看>>
将学习进行到底!为普通人的奋斗送福
查看>>
PHPcms怎么调用二级栏目
查看>>
交换的江湖
查看>>
Docker 快速入门教程
查看>>
FTP基础知识
查看>>
web.xml中的*.jsp如果当welcome-file,eclipse在下次跑的时候不自动更新到tomcat中的问题(eclipse可以去死了)...
查看>>
NettyIO
查看>>
重写重要的库函数
查看>>
NYOJ176 整数划分(二)
查看>>
Spring IoC容器初始化过程学习
查看>>
后缀树
查看>>
Java中的代理
查看>>
顺序表的静态建立
查看>>