Spring-1 之入门
(一)简单对象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 之入门的更多相关文章
- Spring Mvc的入门
SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的. Spring Web MVC是什么: Sprin ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- Spring Boot 快速入门
Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
- Spring Cloud 快速入门
Spring Cloud快速入门 代码地址: https://gitee.com/gloryxu/spring-cloud-test EureKa:服务注册中心 添加依赖 <dependenc ...
- Spring AOP初级——入门及简单应用
在上一篇<关于日志打印的几点建议以及非最佳实践>的末尾提到了日志打印更为高级的一种方式——利用Spring AOP.在打印日志时,通常都会在业务逻辑代码中插入日志打印的语句,这实际上是 ...
- Spring Cloud Gateway入门
1.什么是Spring Cloud GatewaySpring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技 ...
- spring jpetstore研究入门(zz)
spring jpetstore研究入门 分类: java2008-12-21 23:25 561人阅读 评论(2) 收藏 举报 springstrutsibatissearchweb框架servle ...
- Spring Boot -01- 快速入门篇(图文教程)
Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...
随机推荐
- Linux命令之firewall-cmd
firewall-cmd [选项] firewall-cmd是firewalld守护程序的命令行客户端.它提供了管理运行时和永久配置的接口.firewalld中的运行时配置与永久配置分开.这意味着可以 ...
- Web应用主动侦测工具Skipfish
Web应用主动侦测工具Skipfish Skipfish是Kali Linux附带的一个主动Web应用侦测工具.该工具会首先尽可能获取所有网站路径,进行访问,然后根据返回的内容,检测是否存在漏洞. ...
- 【状压dp】Most Powerful
[ZOJ3471]Most Powerful Time Limit: 2 Seconds Memory Limit: 65536 KB Recently, researchers on Ma ...
- BZOJ 3127 [Usaco2013 Open]Yin and Yang(树点分治)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3127 [题目大意] 给出一棵01边权树,求存在多少条路径,使得路径上0和1的数量相同, ...
- 【动态规划】Codeforces Round #406 (Div. 2) C.Berzerk
有向图博弈问题. 能转移到一个必败态的就是必胜态. 能转移到的全是必胜态的就是必败态. 转移的时候可以用队列维护. 可以看这个 http://www.cnblogs.com/quintessence/ ...
- [USACO13NOV]No Change
题目大意: 你有k(k<=16)个硬币,每个硬币都有自己的面值. 现在你要给n件商品付钱,每件商品也有自己的价格. 然而老板是个奸商,他绝对不会给你找钱. 你每次付钱只能用一个硬币,但是你可以一 ...
- java面试笔试总结(一)--亲生经历的面试题
说明:本文只是自己的一些心得体会,答案也是自己写的,正确与否,还需考证 java笔试题 1java笔试题1 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7 ...
- 大于非负整数N的第一个回文数 Symmetric Number
1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 2.样例 1 --> 2 9 -->11 12345 -->12421 123456 --> ...
- 让Code First下的数据库的迁移更加简单
Code First给我们的程序开发带了很多便利,之前的版本中一个比较不大方便的地方是数据库迁移,麻烦不说,往往还和上下文相关,在不同的版本之间的数据库进行迁移还很容易失败,并且一旦失败还不大容易找到 ...
- Express重定向
var express = require('express'); var app = express(); app.get('/',function(req,res){ res.redirect(' ...