给级联属性进行赋值
2018-07-22 05:48:17来源:博客园 阅读 ()
1.创建一个项目,导入jar包(ioc)
2.拷贝applicationContext.xml到src下
3.创建一个bean包,类分别是Book和CartItem,分别提供get,set方法,toStrong和有参,无参
4.在applicationContext.xml进行配置bean,然后进行级联属性赋值
5.进行测试
--------------------------------------------------------------------------------------------------------------------
bean
1 package com.xcz.bean; 2 3 public class Book { 4 private int id; 5 private String name; 6 private int price; 7 public Book() { 8 super(); 9 } 10 public Book(int id, String name, int price) { 11 super(); 12 this.id = id; 13 this.name = name; 14 this.price = price; 15 } 16 public int getId() { 17 return id; 18 } 19 public void setId(int id) { 20 this.id = id; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public int getPrice() { 29 return price; 30 } 31 public void setPrice(int price) { 32 this.price = price; 33 } 34 @Override 35 public String toString() { 36 return "Book [id=" + id + ", name=" + name + ", price=" + price + "]\n"; 37 } 38 39 }
1 package com.xcz.bean; 2 3 public class CartItem { 4 private Book book; 5 private String count; 6 private double amount; 7 8 public CartItem() { 9 super(); 10 } 11 public CartItem(Book book, String count, double amount) { 12 super(); 13 this.book = book; 14 this.count = count; 15 this.amount = amount; 16 } 17 public Book getBook() { 18 return book; 19 } 20 public void setBook(Book book) { 21 this.book = book; 22 } 23 public String getCount() { 24 return count; 25 } 26 public void setCount(String count) { 27 this.count = count; 28 } 29 public double getAmount() { 30 return amount; 31 } 32 public void setAmount(double amount) { 33 this.amount = amount; 34 } 35 @Override 36 public String toString() { 37 return "CartItem [book=" + book + ", count=" + count + ", amount=" + amount + "]\n"; 38 } 39 40 }
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xmlns:jee="http://www.springframework.org/schema/jee" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:aop="http://www.springframework.org/schema/aop" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 12 xsi:schemaLocation=" 13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd 16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 22 <!-- 配置bean --> 23 <bean id="book" class="com.xcz.bean.Book"> 24 <property name="id" value="1"></property> 25 <property name="name" value="红楼梦"></property> 26 <property name="price" value="300"></property> 27 </bean> 28 <!-- 给级联属性赋值 --> 29 <bean id="carItem" class="com.xcz.bean.CartItem"> 30 <property name="book" ref="book"></property> 31 <!-- 赋值 --> 32 <property name="book.id" value="2"></property> 33 <property name="book.name" value="Oracle"></property> 34 <property name="count" value="30"></property> 35 <property name="amount" value="10.2"></property> 36 </bean> 37 </beans>
test
1 package com.xcz.bean.test; 2 3 4 import org.junit.jupiter.api.Test; 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 import com.xcz.bean.CartItem; 9 10 class CarItemTest { 11 //创建IOC容器 12 ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml"); 13 @Test 14 void test1() { 15 CartItem cItem = (CartItem) ioc.getBean("carItem"); 16 System.out.println(cItem); 17 } 18 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:堆排序-Java实现
- Spring事务的传播属性 2020-06-05
- 面对对象(2) 2020-05-28
- Java Stream 流如何进行合并操作 2020-05-12
- 用一张表来存储数据状态,并且可以进行多状态精确查询;使用 2020-04-30
- JSR 303 进行后台数据校验 2020-04-28
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash