您的位置:首页 > 编程语言 > Java开发

Rhythmk 一步一步学 JAVA (12) Spring-1 之入门

2013-11-05 22:01 471 查看
(一)简单对象Spring XML配置说明

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

1.1、新建类UserModel:

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 类:

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>


调用:

@Test
public void Order()
{
BeanFactory factory = new ClassPathXmlApplicationContext("two.xml");
Order order = (Order) factory
.getBean("Order");

order.Show();

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: