• 给级联属性进行赋值


    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 }
    Book
     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 }
    CartItem

    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>
    View Code

    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 }
    View Code
  • 相关阅读:
    《失业的程序员》(十):分歧的产生
    《失业的程序员》(九):创业就是一场戏
    使用MySQL处理百万级以上数据时,不得不知道的几个常识
    《失业的程序员》(八):创业的要素
    《失业的程序员》(七):梦想和胸襟-正文
    《失业的程序员》(六):加班
    《失业的程序员》(五):商战之前
    《失业的程序员》(四):关于猪刚烈
    《失业的程序员》(二):酒后的第一桶金
    《失业的程序员》(三):口才帝和表情帝
  • 原文地址:https://www.cnblogs.com/resultset/p/9346961.html
Copyright © 2020-2023  润新知