Spring-1 之入门

(一)简单对象Spring  XML配置说明

   使用Spring (Spring 3.0) 实现最简单的类映射以及引用,属性赋值:

   1.1、新建类UserModel:

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.spring.ioc_1;
 
/*rhythmk.cnblogs.com*/
public class UserModel {
 
    public int getAge() {
        return Age;
    }
    public void setAge(int age) {
        Age = age;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    private int Age;
    private String  Name;
    private Apple apple;
    public Apple getApple() {
        return apple;
    }
    public void setApple(Apple apple) {
        this.apple = apple;
    }
    public void Info()
    {
        System.out.println(String.format("我的姓名是%s,我的年纪是%s",this.Name,this.Age ));
    }
     
     
    public void Say(String msg)
    {
        System.out.println(String.format("“%s”说:%s!",this.Name,msg));
    }
     
     
    public void Eat()
    {
     
        System.out.println(String.format("“%s”正在吃%s的苹果!",this.Name,this.apple.getColor()));
    }
}

  Apple 类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.spring.ioc_1;
 
public class Apple {
    private String Color;
 
    public String getColor() {
        return Color;
    }
 
    public void setColor(String color) {
        Color = color;
    }
 
 
}

  

1.2、Spring配置文件:

    one.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-2.5.xsd">
        

        
    <bean id="apple" class="com.spring.ioc_1.Apple">
    <!-- 通过 property-Name 以及 value  映射对应的setPropretyName方法 赋值  -->
    <property name="Color" value="红色"></property>
    </bean>
    
    <bean  id="userModel" class="com.spring.ioc_1.UserModel">
      <property name="Age" > <value> 12</value></property>
      <property name="Name"><value>rhythmk</value> </property>
      <!-- ref 引用对应的 指定 id 的bean -->
      <property name="Apple" ref="apple"></property>
    </bean>
    
    
</beans> 
复制代码

1.3、调用

复制代码
@Test
    public void UserSay()
    {
        BeanFactory factory = new ClassPathXmlApplicationContext("one.xml");
        UserModel userModel = (UserModel) factory
                .getBean("userModel");
        
        userModel.Say("Hello");
        userModel.Info();
        userModel.Eat();
        
    }
复制代码

输出:

“rhythmk”说:Hello!
我的姓名是rhythmk,我的年纪是12
“rhythmk”正在吃红色的苹果!

 

(二)Map,Set,List,Properties  XML配置说明

2.1、Order 类

复制代码
package com.spring.ioc_1;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


public class Order {

    // 商品集合
    private Map goods;
    public Map getGoods() {
        return goods;
    }
    public void setGoods(Map goods) {
        this.goods = goods;
    }
    public Set getGoodsType() {
        return goodsType;
    }
    public void setGoodsType(Set goodsType) {
        this.goodsType = goodsType;
    }
    public List getOrderType() {
        return orderType;
    }
    public void setOrderType(List orderType) {
        this.orderType = orderType;
    }
    public Properties getPrice() {
        return price;
    }
    public void setPrice(Properties price) {
        this.price = price;
    }
    // 包括物品种类
    private Set goodsType;
    // 订单类型
    private List orderType;
    //  物品价格
    private Properties price;
    
    public void Show()
    {
        System.out.println("订单创建完成:");
                // **  输出属性******
    }
    
    
    
}
复制代码

 

2.2、 属性配置 :Two.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-2.5.xsd">

    <bean id="Order" class="com.spring.ioc_1.Order">
        <property name="goods">
            <map>
                <entry key="0001">
                    <value>啤酒</value>
                </entry>
                <entry key="0002">
                    <value>电脑</value>
                </entry>
            </map>
        </property>

        <property name="goodsType">
            <set>
                <value>虚拟订单</value>
                <value>百货</value>
                <value>图书</value>
            </set>
        </property>
        <property name="price">
            <props>
                <prop key="pj001">2.3</prop>

                <prop key="dn001">1232.3</prop>
            </props>

        </property>
        <property name="orderType">
            <list>
                <value>虚拟货物</value>
                <value>生活用品</value>
                <value>书</value>
            </list>
        </property>

    </bean>


</beans> 
复制代码

调用:

1
2
3
4
5
6
7
8
9
10
@Test
   public void Order()
   {
       BeanFactory factory = new ClassPathXmlApplicationContext("two.xml");
       Order order = (Order) factory
               .getBean("Order");
        
       order.Show();
        
   }
posted @ 2016-04-17 22:21  starskyhu  阅读(260)  评论(0编辑  收藏  举报