实体类:

package com.zxy.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}

 

package com.zxy.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

 

package com.zxy.pojo;

public class User {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 

ApplictionContext.xml(这里用beans.xml简化了)

 1 <?xml version="1.0" encoding="UTF8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="
 5         http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
 7 
 8     <bean id="address" class="com.zxy.pojo.Address"/>
 9 
10     <bean id="student" class="com.zxy.pojo.Student">
11         <!--  普通值注入  value      -->
12         <property name="name" value="钟悉洋"/>
13         <!--  Bean注入      -->
14         <property name="address" ref="address"/>
15         <!--  数组注入,ref      -->
16         <property name="books">
17             <array>
18                 <value>红楼梦</value>
19                 <value>西游记</value>
20                 <value>三国演义</value>
21                 <value>水浒传</value>
22             </array>
23         </property>
24         <!-- List注入       -->
25         <property name="hobbys">
26             <list>
27                 <value>听歌</value>
28                 <value>敲代码</value>
29                 <value>看电影</value>
30             </list>
31         </property>
32         <!-- Map注入       -->
33         <property name="card">
34             <map>
35                 <entry key="身份证" value="123123123123123"/>
36                 <entry key="银行卡" value="97897897897"/>
37             </map>
38         </property>
39         <!--Set注入        -->
40         <property name="games">
41             <set>
42                 <value>LOL</value>
43                 <value>COC</value>
44                 <value>BOB</value>
45             </set>
46         </property>
47         <!-- null注入       -->
48         <property name="wife">
49             <null/>
50         </property>
51         <!-- Properties注入       -->
52         <property name="info">
53             <props>
54                 <prop key="driver">mysql.jdbc</prop>
55                 <prop key="url">.....</prop>
56                 <prop key="username">root</prop>
57                 <prop key="password">123456789</prop>
58             </props>
59         </property>
60 
61     </bean>
62 </beans>

 

 1 <?xml version="1.0" encoding="UTF8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:p="http://www.springframework.org/schema/p"
 5        xmlns:c="http://www.springframework.org/schema/c"
 6        xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
 9 
10 
11         <!-- p命名 直接赋值  需要导入约束: xmlns:p="http://www.springframework.org/schema/p" -->
12         <bean id="user" class="com.zxy.pojo.User" p:name="钟悉洋" p:age="20"/>
13 
14         <!-- c命名 有参构造器 construct-args 需要导入约束: xmlns:c="http://www.springframework.org/schema/c" -->
15         <bean id="user2" class="com.zxy.pojo.User" c:age="18" c:name="啧啧啧"/>
16 </beans>

 

Test

import com.zxy.pojo.Student;
import com.zxy.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student =  context.getBean("student",Student.class);
        System.out.println(student.toString());

    }
    @Test
    public void test01(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user", User.class);
        System.out.println(user);
        User user2 = context.getBean("user2", User.class);
        System.out.println(user2);
    }
}

 

原文地址:http://www.cnblogs.com/kidzxy/p/16836477.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性