HierarchicalBeanFactory演示 : XML组件 « Spring « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » Spring » XML组件屏幕截图 
HierarchicalBeanFactory演示

       
File: injectdemo2-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="name" class="java.lang.String">
        <constructor-arg value="My Value 1"/>
    </bean>

    <bean id="injectSimpleParent" class="InjectSimpleDemo">
        <property name="name" ref="name"/>
        <property name="age" value="2"/>
        <property name="height" value="0.8"/>
        <property name="isProgrammer" value="false"/>
        <property name="ageInSeconds" value="63072000"/>
    </bean>

</beans>


File: injectdemo3-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="name" class="java.lang.String">
        <constructor-arg value="My value"/>
    </bean>

    <bean id="injectSimpleChild" class="InjectSimpleDemo">
        <property name="name" ref="name"/>
        <property name="age" value="2"/>
        <property name="height" value="0.8"/>
        <property name="isProgrammer" value="false"/>
        <property name="ageInSeconds" value="63072000"/>
    </bean>

    <bean id="injectSimpleChild2" class="InjectSimpleDemo">
        <property name="name">
            <ref parent="name"/>
        </property>
        <property name="age" value="2"/>
        <property name="height" value="0.8"/>
        <property name="isProgrammer" value="false"/>
        <property name="ageInSeconds" value="63072000"/>
    </bean>

</beans>


File: Main.java

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {

  public static void main(String[] args) {
    XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("injectdemo2-context.xml"));
    XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("injectdemo3-context.xml"), parent);

    System.out.println(parent.getBean("injectSimpleParent"));
    System.out.println(child.getBean("injectSimpleChild"));
    System.out.println(child.getBean("injectSimpleChild2"));  }
}

class InjectSimpleDemo {
  private String name;

  private int age;

  private float height;

  private boolean isProgrammer;

  private Long ageInSeconds;

  public void setAgeInSeconds(Long ageInSeconds) {
    this.ageInSeconds = ageInSeconds;
  }

  public void setIsProgrammer(boolean isProgrammer) {
    this.isProgrammer = isProgrammer;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public void setHeight(float height) {
    this.height = height;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return String.format("Name: %s\nAge: %d\nAge in Seconds: %d\nHeight: %g\nIs Programmer?: %b",
        this.name, this.age, this.ageInSeconds, this.height, this.isProgrammer);
  }
}




           
       
Spring-HierarchicalBeanFactoryDemo.zip( 2,600 k)
Related examples in the same category
1. XML的组件注射
2. 参考另一组件和设定财产
3. 静态工厂
4. 搜寻基包
5. throw RequiredPropertyNotSetException
6. 基于属性文件Spring组件
7. 非静态工厂
8. 当地参考
9. 联系到DataSource
10. 继承演示
11. 过滤诠释
12. 销毁方法
13. 依赖检查演示
14. 自定义InitializationMethod
15. 部分扫描
16. 扫描组件和范围
17. 组件过滤器分配
18. 实施BeanNameAware
19. 初始化的组件生命周期
20. 组件生命周期DisposableBean
21. Autowiring
22. 别名组件演示
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.