zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    你不该知道的.NET 第零回: 不继承Object的后果 不及格的程序员
    开张 不及格的程序员
    用WinDBG调试器 修改 星际争霸 等游戏. 不及格的程序员
    自定义服务器控件 继承不到父类/基类的 SupportsEventValidation 特性. 不及格的程序员
    讨论 计算机 操作系统休眠恢复的过程. 不及格的程序员
    十一期间 极品飞车13:变速 通关了 不及格的程序员
    谁发明的 Ctrl + Alt + Del 组合键,以及它在Windows中的重要性. 不及格的程序员
    asp.net development server 挂起问题解决
    SqlServer数据库记录数大引起的一系列问题解决
    完全分布模式hadoop集群安装配置之一安装第一个节点
  • 原文地址:https://www.cnblogs.com/davidwang456/p/6019610.html
Copyright © 2011-2022 走看看