(一)简单对象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(); }

  

Rhythmk 一步一步学 JAVA (12) Spring-1 之入门的更多相关文章

  1. 从零开始学 Java - 搭建 Spring MVC 框架

    没有什么比一个时代的没落更令人伤感的了 整个社会和人都在追求创新.进步.成长,没有人愿意停步不前,一个个老事物慢慢从我们生活中消失掉真的令人那么伤感么?或者说被取代?我想有些是的,但有些东西其实并不是 ...

  2. 教妹学Java:Spring 入门篇

    你好呀,我是沉默王二,一个和黄家驹一样身高,刘德华一样颜值的程序员(管你信不信呢).从两位偶像的年纪上,你就可以断定我的码龄至少在 10 年以上,但实话实说,我一直坚信自己只有 18 岁,因为我有一颗 ...

  3. Rhythmk 一步一步学 JAVA (10): Freemarker 学习 1 - 入门

    FreeMarker 笔记: 1. 注释:   <#-- 注释内容 -#> 2.<#if condition> content1 <#else> content2 ...

  4. 从零开始学 Java - 搭建 Spring MVC 记录云创的日子 第一章

    2017年11月29日 来到新项目,需要用到Spring MVC ,那么我就开始记录我这次的学习. Spring MVC 框架是围绕一个 DispatcherServlet 来设计的,这个 Servl ...

  5. 一步一步学Silverlight 2系列(12):数据与通信之WebClient

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. usb-host一步一步学(二)安卓在usb-host模式下列出当前连接的usb设备

    之前写了一个简单的例子usb-host一步一步学(一)安卓在usb-host模式下列出当前连接的usb设备,下面的这个例子是获取各种usb设备.usb接口以及usb连接点(endpoint) 正如上一 ...

  7. 12天,这本《重学Java设计模式》PDF书籍下载量9k,新增粉丝1400人,Github上全球推荐榜!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言

  8. 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计

    本帖最后由 xinxincaijq 于 2013-1-9 10:27 编辑 一步一步学ZedBoard & Zynq(四):基于AXI Lite 总线的从设备IP设计 转自博客:http:// ...

  9. 一步一步学android控件(之十五) —— DegitalClock & AnalogClock

    原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...

随机推荐

  1. OK335xS knob driver hacking

    /************************************************************************* * OK335xS knob driver hac ...

  2. 【剑指offer】09-2跳台阶,C++实现

    原创博文,转载请注明出处! # 本文是牛客网<剑指offer>刷题笔记 1.题目 # 一只青蛙一次可以跳1级台阶,也可以跳2级.求该青蛙跳n级的台阶总共有多少种跳法. 2.思路 # 跳0级 ...

  3. typecho去index.php

    RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} ! ...

  4. [转]RC4加密已不再安全,破解效率极高

    原文链接:http://freebuf.com/news/72622.html 原文发表时间:2015.7.17 安全研究人员称,现在世界上近三分之一的HTTPS加密连接可被破解,并且效率极高.这种针 ...

  5. 20165212 学习基础和C语言基础调查

    学习基础与C语言基础调查 阅读文章的心得体会 五篇文章都从不同角度阐述了毅力的重要性,打字.减肥.运动各方面,比如“每天一万步”这个任务,人们通过建群聊互相监督.“打卡”的方式来实现坚持的目的,我认为 ...

  6. WPF中Grid实现网格,表格样式通用类(转)

    /// <summary> /// 给Grid添加边框线 /// </summary> /// <param name="grid"></ ...

  7. curl 错误 [globbing] illegal character in range specification at pos

    现象 在使用curl 进行ipv6请求的时候 curl -v "http://[1:1::1]/test.html" 发生了一个错误,报错是 [globbing] illegal ...

  8. 解决EditPlus的默认编码方式有关问题(转)

    http://blog.csdn.net/hzhsan/article/details/7911660 最近在使用英文版的Editplus写代码的时候,发现中文字符在调试过程中都变成了乱码, 发现是E ...

  9. 常用 Git 命令使用教程

    下面整理一下自己在开发过程中经常使用到的 Git 命令.使用 GUI 工具的同学,也可以对照起来看看. Git 配置 1. 在安装完成 Git 后,开始正式使用前,是需要有一些全局设置的,如用户名.邮 ...

  10. 使用JDK合成照片

    原图(工程所在目录7098849.jpg): 头像(工程所在目录20181023201750.jpg): 开始合成(执行如下main方法): public static void main(Strin ...