加载属性文件 : 属性注射 « Spring « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » Spring » 属性注射 
28. 5. 24. 加载属性文件

File: 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"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

    <bean id="transactionManager" class="MyTransactionManager"/>

    <util:constant id="X" static-field="java.lang.Integer.MAX_VALUE"/>

    <util:list id="Y" list-class="java.util.ArrayList">
        <value>value1</value>
        <ref bean="X"/>
    </util:list>

    <util:list id="greetingsList">
        <value>Hello, world</value>
        <value>How are you doing today?</value>
    </util:list>

    <util:map id="Z" map-class="java.util.HashMap">
        <entry key="x" value="y"/>
        <entry key="y"><ref bean="X"/></entry>
    </util:map>

    <util:properties id="P" location="classpath:Main.properties"/>

    <bean id="simple" class="SimpleBean"/>

    <util:property-path id="Q" path="simple.name"/>

    <util:set id="S" set-class="java.util.HashSet">
        <value>foo</value>
        <ref bean="X"/>
    </util:set>

</beans>

File: Main.java

import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.AbstractPlatformTransactionManager;
import org.springframework.transaction.support.DefaultTransactionStatus;

public class Main {
  private static void printMap(Map z) {
    for (Object o : z.entrySet()) {
      Map.Entry e = (Map.Entryo;
      System.out.println(e.getKey() " => " + e.getValue().getClass() " " + e.getValue());
    }
  }

  private static void printList(List y) {
    for (Object o : y) {
      System.out.println(o.getClass() " " + o);
    }
  }

  public static void main(String[] argsthrows Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/context.xml");

    List y = (Listcontext.getBean("Y");
    printList(y);

    Map z = (Mapcontext.getBean("Z");
    printMap(z);

    Map p = (Mapcontext.getBean("P");
    printMap(p);
  }
}

class MyTransactionManager extends AbstractPlatformTransactionManager {

  @Override
  protected Object doGetTransaction() throws TransactionException {
    System.out.println("doGetTransaction");
    return new Object();
  }

  @Override
  protected void doBegin(Object object, TransactionDefinition transactionDefinition)
      throws TransactionException {
    System.out.println("doBegin");
  }

  @Override
  protected void doCommit(DefaultTransactionStatus defaultTransactionStatus)
      throws TransactionException {
    System.out.println("doCommit");
  }

  @Override
  protected void doRollback(DefaultTransactionStatus defaultTransactionStatus)
      throws TransactionException {
    System.out.println("doRollback");
  }
}

class SimpleBean {
  private String name;

  private String value;

  public SimpleBean() {
    this.name = "My name";
    this.value = "My value";
  }

  public String getName() {
    return name;
  }

  public String getValue() {
    return value;
  }
}

File: Main.properties

foo=bar
baz=foobar
  Download:  Spring-LoadpropertyFileInContext.zip( 4,651 k)
28. 5. 属性注射
28. 5. 1. 填补映射
28. 5. 2. 集赋值
28. 5. 3. 填写属性
28. 5. 4. 填写清单到另一个列表
28. 5. 5. 填补日历对象
28. 5. 6. 属性设置:日期
28. 5. 7. 属性设置字节数组
28. 5. 8. 属性设定布尔
28. 5. 9. 属性设定:BigDecimal
28. 5. 10. 属性设定属性
28. 5. 11. 属性设定整数
28. 5. 12. 财产设定文件
28. 5. 13. 属性设定类类型
28. 5. 14. 属性设定流网址
28. 5. 15. 属性设定:网址
28. 5. 16. 属性设定字符串数组
28. 5. 17. Fill Value To List
28. 5. 18. 创建列表,映射
28. 5. 19. 组件注射:映射
28. 5. 20. 组件注射:收藏集
28. 5. 21. 组件注射:集属性
28. 5. 22. 组件注射:收集映射
28. 5. 23. 组件注射:集合清单
28. 5. 24. 加载属性文件
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.