(一)简单对象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. HttpClient(转载)

    Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我 们再讨论),它不仅是客户端发送Http请求变得容易,而 ...

  2. Ajax使用进阶

    关于Ajax的概念不再做解释了,我想通过三个小例子来让大家对Ajax有个清晰的认识.要学习它,必须从最基础最原始的方式开始认识,然后通过使用框架来提升效率,逐步认识它. 一.原生js版(注册的用户名是 ...

  3. 多进程失败拉起的demo

    #include <iostream> #include <vector> #include <unistd.h> #include <stdlib.h> ...

  4. [BZOJ1444]有趣的游戏(AC自动机+矩阵乘法)

    n个等长字符串,机器会随机输出一个字符串(每个字母出现的概率为p[i]),问每个字符串第一个出现的概率是多少. 显然建出AC自动机,套路地f[i][j]表示i时刻位于节点j的概率. 构建转移矩阵,当i ...

  5. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  6. 【后缀自动机】CDOJ1551 Hesty Str1ng

    可以发现,对于原串的每个长度>1的子串而言,将其除了最后一个字符之外反向接在其结尾,都是一个合法解.该解的长度一定是奇数. 对于原串的每个长度>2,且结尾两个字符相同的子串而言,将其除了最 ...

  7. Sata win7 热插拔(AHCI)

    主板支持AHCI,把sata模式改成AHCI,在bios打开SATA热插拔开关 开启AHCI,需要修改注册表:HKEY_LOCAL_MACHINE\System\CurrentControlSet\S ...

  8. 高级进阶DB2(第2版)

    <高级进阶DB2(第2版)> 基本信息 作者: 牛新庄 出版社:清华大学出版社 ISBN:9787302323839 上架时间:2013-7-3 出版日期:2013 年7月 开本:16开 ...

  9. 跟着我从零开始入门FPGA(一周入门XXOO系列)-1、Verilog语法

    (本连载共七部分,这是第一部分) 作者:McuPlayer2013   (EETOP FPGA版块版主) 原帖地址:http://bbs.eetop.cn/thread-385362-1-1.html ...

  10. 流畅的python第四章文本和字节序列学习记录

    字符问题 把码位转化成字节序列的过程是编码,把字节序列转化成码位的过程是解码 把unicode字符串当成人类可读的文本,码位当成机器可读的, 将字节序列编程人类可读是解码,把字符串编码成字节序列是编码 ...