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

Spring-1 之入门的更多相关文章

  1. Spring Mvc的入门

    SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的. Spring Web MVC是什么: Sprin ...

  2. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  3. [Spring框架]Spring AOP基础入门总结一.

    前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...

  4. Spring Boot 快速入门

    Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...

  5. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

  6. Spring Cloud 快速入门

     Spring Cloud快速入门 代码地址: https://gitee.com/gloryxu/spring-cloud-test EureKa:服务注册中心 添加依赖 <dependenc ...

  7. Spring AOP初级——入门及简单应用

      在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...

  8. Spring Cloud Gateway入门

    1.什么是Spring Cloud GatewaySpring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技 ...

  9. spring jpetstore研究入门(zz)

    spring jpetstore研究入门 分类: java2008-12-21 23:25 561人阅读 评论(2) 收藏 举报 springstrutsibatissearchweb框架servle ...

  10. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

随机推荐

  1. Sqli-labs less 6

    Less-6 Less6与less5的区别在于less6在id参数传到服务器时,对id参数进行了处理.这里可以从源代码中可以看到. $id = '"'.$id.'"'; $sql= ...

  2. 进入CentOS7紧急模式恢复root密码

    第一步.重启CentOS7,在以下界面选择要编辑的内核(一般第一个),按e进入编辑界面 第二步.在编辑界面找到如下一行,将ro改为rw init=/sysroot/bin/sh.改完后<Ctrl ...

  3. 【kubernetes】ubuntu14.04 64位 搭建kubernetes过程

    背景: Kubernetes介绍:http://kubernetes.io/docs/getting-started-guides/ github地址:https://github.com/kuber ...

  4. grunt-contrib-qunit安装过程中phantomjs安装报错问题解决

    今天自己fork了一个github上别人写的一个关于grunt项目的一个小demo(https://github.com/cowboy/jquery-tiny-pubsub),主要是想学习下grunt ...

  5. C#代码规范化(代码风格化)的几个函数

    近期由于适配Oracle的缘故,将旺财C#.NET代码生成器增加了风格化的几个函数,具体实现如下功能: 1.转换为Pascal风格,如User_Name/USER_NAME/UserName自动替换下 ...

  6. Java 生产者消费者 & 例题

    Queue http://m635674608.iteye.com/blog/1739860 http://www.iteye.com/problems/84758 http://blog.csdn. ...

  7. 【数论】【Polya定理】poj1286 Necklace of Beads

    Polya定理:设G={π1,π2,π3........πn}是X={a1,a2,a3.......an}上一个置换群,用m中颜色对X中的元素进行涂色,那么不同的涂色方案数为:1/|G|*(mC(π1 ...

  8. Java中的文件操作(一)RandomAccessFile

    今天,学到的是java中的文件操作. Java.IO.File Java中操作文件用到RandomAccessFile类,既可以读取文件内容,也可以向文件输出数据,但不同与普通输入/输出流的是Rand ...

  9. Scala 数据类型

    Scala 与 Java有着相同的数据类型,下表列出了 Scala 支持的数据类型: Byte8位有符号补码整数.数值区间为 -128 到 127 Short16位有符号补码整数.数值区间为 -327 ...

  10. #iOS问题记录# UIWebView滑动到底部

    最近看Tmall的iOS APP,在Tmall的商品简介页面,当拖动到最底部时,会提示继续向上拖动,“查看图文详情”: 觉得这个设计挺好的.闲来无事,自己UIWebView模仿一下,问题是检查UIWe ...