(一)简单对象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. 并发系列5-大白话聊聊Java并发面试问题之微服务注册中心的读写锁优化【石杉的架构笔记】

  2. 洛谷—— P2562 [AHOI2002]Kitty猫基因编码

    P2562 [AHOI2002]Kitty猫基因编码 题目描述 小可可选修了基础生物基因学.教授告诉大家 Super Samuel 星球上 Kitty猫的基因的长度都是 2 的正整数次幂 ), 全是由 ...

  3. 【Go】基础语法之接口

    接口定义: 利用关键字interface来定义一个接口,接口是一组方法的集合. 例如: type People interface { Show(name string, age int) (id i ...

  4. 渗透脚本快速生成工具Intersect

    渗透脚本快速生成工具Intersect   当渗透人员获取目标系统的执行权限,往往需要编写相应的脚本,实现更多的渗透操作.Kali Linux提供一款Python脚本快速生成工具Intersect.该 ...

  5. VB程序打包方法之如何在发布安装之后不带源码

    很久之前,我发表了一片博客是VB程序如何打包,在那里面我总结了两个方法.有兴趣可以看看我的这篇博客http://blog.csdn.net/lu930124/article/details/88467 ...

  6. 【找规律】计蒜客17118 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 E. Maximum Flow

    题意:一张有n个点的图,结点被编号为0~n-1,i往所有编号比它大的点j连边,权值为i xor j.给你n,问你最大流. 打个表,别忘了把相邻两项的差打出来,你会发现神奇的规律……你会发现每个答案都是 ...

  7. Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法

    F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...

  8. Apache 优化配置10条建议

    之前VPS使用的是默认的Apache配置,感觉还行,不过随着博客的人流量上升,显然这种配置无法满足需求了:下面是Apache官方手册中给出的几条优化配置建议,笔者将其整理出来,对Apache服务器的运 ...

  9. CSS揭秘之多重边框&连续的图像边框

    1.多重边框 我们可以通过使用border-image来写一个多重边框,或使用多个元素来模拟多重边框,不过我们有更好的办法来制作一个多重边框,那就是使用box-shadow的第四个参数(称为扩张半径) ...

  10. OpenShift蓝绿及灰度部署

    内容转自https://blog.csdn.net/jj_tyro/article/details/80136316, 并不断补充,感谢作者. 1.蓝绿部署 蓝绿部署实现的是全流量切换,适合于在测试完 ...