Rhythmk 一步一步学 JAVA (12) Spring-1 之入门
2024-09-02 09:37:09
(一)简单对象Spring XML配置说明
使用Spring (Spring 3.0) 实现最简单的类映射以及引用,属性赋值:
1.1、新建类UserModel:
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 类:
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>
调用:
@Test
public void Order()
{
BeanFactory factory = new ClassPathXmlApplicationContext("two.xml");
Order order = (Order) factory
.getBean("Order"); order.Show(); }
最新文章
- List中存储同一个对象(内容不同)
- JavaScript学习06 JS事件对象
- Maven的作用总结
- 【bzoj2333】 [SCOI2011]棘手的操作 可并堆+lazy标记
- 字符串匹配算法之SimHash算法
- POJ 2823 Sliding Window 再探单调队列
- Java基础毕向东day05 对象与对象的区别,匿名内部类,函数的执行流程。
- 转:Move all SQL Server system databases at one time
- .net remoting 客户端与服务端绑定事件,一部电脑当服务器,另一部当客户端,发布后没法接收远程错误信息。
- request对象
- 添加标签2 jquery 和JS
- java 泛型简单使用
- Linux 系统应用编程——标准I/O
- 视频当道的时代,这些珍藏的优质 Python 播客值得推荐
- POI生成EXCEL文件
- 微服务Kong(一)——简介
- hdu 1397 (素数判定)
- iOS 隐私政策
- 搞懂分布式技术12:分布式ID生成方案
- Centos7搭建SVN Server手记
热门文章
- vue-route(三)后台管理路由配置
- Redis学习总结之三——Redis客户端命令
- 每天一个linux命令:【转载】ls命令
- 20179223《Linux内核原理与分析》第二周学习笔记
- FirstTry_HelloWorld
- Java8新特性——StreamAPI(一)
- PHP 生成类似 SqlServer NEWID() 全局唯一标识符
- 《DSP using MATLAB》示例Example7.9
- HashMap,LinkedHashMap,TreeMap之间的区别
- 实例-QPSK的fpga实现