简单注射姓名 : IOC映射 « 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 » IOC映射屏幕截图 
简单注射姓名


/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/



///////////////////////////////////////////////////////////////////////////////////////
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <!-- injecting built-in vals sample -->
    <bean id="injectSimple" class="InjectSimple">
        <property name="name">
            <value>John Smith</value>
        </property>
        <property name="age">
            <value>35</value>
        </property>
        <property name="height">
            <value>1.78</value>
        </property>
        <property name="isProgrammer">
            <value>true</value>
        </property>
        <property name="ageInSeconds">
            <value>1103760000</value>
        </property>
    </bean>
    
    <!-- oracle bean used for a few examples -->
    <bean id="oracle" name="wiseworm" class="BookwormOracle"/>
    
    <!-- injecting reference sample (using id)
    <bean id="injectRef" class="InjectRef">
        <property name="oracle">
            <ref local="oracle"/>
        </property>
    </bean>
     -->
    <!-- inject ref sample (using name-->
    <bean id="injectRef" class="InjectRef">
        <property name="oracle">
            <ref bean="wiseworm"/>
        </property>
    </bean>
</beans>



///////////////////////////////////////////////////////////////////////////////////////
public interface Oracle {

    public String defineMeaningOfLife();
}



///////////////////////////////////////////////////////////////////////////////////////


public class BookwormOracle implements Oracle {

    private Encyclopedia enc;

    public void setEncyclopedia(Encyclopedia enc) {
        this.enc = enc;
    }

    public String defineMeaningOfLife() {
        return "Encyclopedia's are a waste of money - use the Internet";
    }

}



///////////////////////////////////////////////////////////////////////////////////////

public class Encyclopedia {

}


///////////////////////////////////////////////////////////////////////////////////////


public class InjectRef {

    private Oracle oracle;
    
    public void setOracle(Oracle oracle) {
        this.oracle = oracle;
        System.out.println(oracle.defineMeaningOfLife());
    }
}

///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class InjectSimple {

    private String name;

    private int age;

    private float height;

    private boolean isProgrammer;

    private Long ageInSeconds;

    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
                "build/beans.xml"));
        InjectSimple simple = (InjectSimple)factory.getBean("injectSimple");
        factory.getBean("injectRef");
        System.out.println(simple);
    }

    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;
    }

    public String toString() {
        return    "Name :" + name + "\n"
                "Age:" + age + "\n"
                "Age in Seconds: " + ageInSeconds + "\n"
                "Height: " + height + "\n"
                "Is Programmer?: " + isProgrammer;
    }
}
           
       
SimpleInjectByName.zip( 1,200 k)
Related examples in the same category
1. 简单的注射产品编号
2. 自动布线
3. 依赖检查
4. 世界您好XML与DI
5. 查询性能
6. Spring中模型视图注射
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.