Rhythmk 一步一步学 JAVA (12) Spring-1 之入门
2013-11-05 22:01
471 查看
(一)简单对象Spring XML配置说明
使用Spring (Spring 3.0) 实现最简单的类映射以及引用,属性赋值:
1.1、新建类UserModel:
Apple 类:
1.2、Spring配置文件:
one.xml
1.3、调用
输出:
“rhythmk”说:Hello!
我的姓名是rhythmk,我的年纪是12
“rhythmk”正在吃红色的苹果!
(二)Map,Set,List,Properties XML配置说明
2.1、Order 类
2.2、 属性配置 :Two.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(); }
相关文章推荐
- Java 正则表达式(二)
- Java上传、下载代码
- NetBeans更改默认JDK路径
- SpringMVC和Struts的区别
- SpringMVC和Struts是线程安全的吗?为什么?
- Struts2注解Convention扫描jar中的Action的设置,注解Action咋jar中找不到.
- Java基础1--概述--环境变量--HelloWorld
- java socket编程
- Java中File类使用
- JAVA NIO 简介
- Java 中的语法糖 (Syntactic Sugar)
- 不用Spring也能实现Ioc
- JAVA多线程
- Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/slf4j/impl/Static
- Hibernate配置错误(javax.persistence.Cacheable)
- Struts2+Hibernate+Spring整合(一)
- 修改struts2 中 struts.xml 默认路径
- 修改Struts2的后缀名
- javabeans的运用
- 跨平台获取java进程id(Process ID in Java)