(一)简单对象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. go chapter 2 - read file(yaml)

    func main() { data, err := ioutil.ReadFile("D:/test/widua.go") if err != nil { fmt.Println ...

  2. SublimeCodeIntel代码自动补全配置

    主要使用python3,所有配置以python3为例.其他语言同理.利用sublimeCodeIntel插件可以实现自动提示python3代码.跳转追踪自定义函数.查看系统函数等.功能还是相当强大的. ...

  3. luogu P3375 【模板】KMP字符串匹配

    题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...

  4. [NOIP2017]时间复杂度(模拟)

    sscanf读入数字,getline(cin,string)读一整行,其余暴力模拟即可. #include<cstdio> #include<string> #include& ...

  5. BZOJ 2705 [SDOI2012]Longge的问题(欧拉函数)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2705 [题目大意] 求出∑gcd(i,N)(1<=i<=N) [题解] $ ...

  6. 【枚举】【尺取法】hdu6103 Kirinriki

    两个等长字符串A,B的距离被定义为 给你一个字符串,问你对于所有长度相等的不相交子串对,其距离不超过m的前提下,最长的长度是多少. 枚举对称轴,两侧先贪心地扩展到最长,超过m之后,再缩短靠近对称轴的端 ...

  7. [CF843D]Dynamic Shortest Path

    [CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...

  8. MYSQL复习笔记1-物理文件和系统架构

    date:20140101auth:Jin 一.物理组成(一) 日志文件参考:http://dev.mysql.com/doc/refman/5.1/en/server-logs.html1.错误日志 ...

  9. GG同步到sqlserver报错一例 Invalid date format

    在将Oracle表同步到sqlserver时,在sqlserver端应用数据时,可能会遇到这个报错. 2014-05-17 17:20:24 WARNING OGG-01154 SQL error - ...

  10. eval命令的使用

    eval :     将参数组合为一个新的命令,然后交给Shell执行.读取一连串的参数,并依参数本身的内容来执行命令.常用来处理Shell脚本中的变量,参数不限数目,参数用分号";&quo ...