1、通过Spring创建对象,现有Users和Cart实体类,关系为1:1

  属性注入的方式给Users属性赋值

  2、Cart和Product实体类,关系1:n

  构造器注入的方式给Cart属性赋值

  Cart中包含Set<Product>、Map<String,Product>

 1 package com.spring.entity;
2
3 public class Users {
4 private int id;
5
6 private String username;
7
8 private String password;
9
10 private Cart cart;
11
12 public Cart getCart() {
13 return cart;
14 }
15
16 public void setCart(Cart cart) {
17 this.cart = cart;
18 }
19
20 public int getId() {
21 return id;
22 }
23
24 public void setId(int id) {
25 this.id = id;
26 }
27
28 public String getUsername() {
29 return username;
30 }
31
32 public void setUsername(String username) {
33 this.username = username;
34 }
35
36 public String getPassword() {
37 return password;
38 }
39
40 public void setPassword(String password) {
41 this.password = password;
42 }
43
44 @Override
45 public String toString() {
46 return "Users{" +
47 "id=" + id +
48 ", username='" + username + '\'' +
49 ", password='" + password + '\'' +
50 ", cart=" + cart +
51 '}';
52 }
53 }

Users

 1 package com.spring.entity;
2
3 import java.util.Map;
4 import java.util.Set;
5
6 public class Cart {
7 private int id;
8 private int uid;
9 private int pcount;
10 private Set<Product> productSet;
11 private Map<String,Product> productMap;
12
13 public int getId() {
14 return id;
15 }
16
17 public void setId(int id) {
18 this.id = id;
19 }
20
21 public int getUid() {
22 return uid;
23 }
24
25 public void setUid(int uid) {
26 this.uid = uid;
27 }
28
29 public int getPcount() {
30 return pcount;
31 }
32
33 public void setPcount(int pcount) {
34 this.pcount = pcount;
35 }
36
37 public Set<Product> getProductSet() {
38 return productSet;
39 }
40
41 public void setProductSet(Set<Product> productSet) {
42 this.productSet = productSet;
43 }
44
45 public Map<String, Product> getProductMap() {
46 return productMap;
47 }
48
49 public void setProductMap(Map<String, Product> productMap) {
50 this.productMap = productMap;
51 }
52
53 @Override
54 public String toString() {
55 return "Cart{" +
56 "id=" + id +
57 ", uid=" + uid +
58 ", pcount=" + pcount +
59 ", productSet=" + productSet +
60 ", productMap=" + productMap +
61 '}';
62 }
63 }

Cart

 1 package com.spring.entity;
2
3 public class Product {
4 private Integer id;
5
6 private String pname;
7
8 private Double pprice;
9
10 private Integer pinventory;
11
12 private String picon;
13
14 private String plocation;
15
16 private Integer pviews;
17
18 public Product() {
19 }
20
21 public Product(Integer id, String pname, Double pprice, Integer pinventory, String picon, String plocation, Integer pviews) {
22 this.id = id;
23 this.pname = pname;
24 this.pprice = pprice;
25 this.pinventory = pinventory;
26 this.picon = picon;
27 this.plocation = plocation;
28 this.pviews = pviews;
29 }
30
31 @Override
32 public String toString() {
33 return "Product{" +
34 "id=" + id +
35 ", pname='" + pname + '\'' +
36 ", pprice=" + pprice +
37 ", pinventory=" + pinventory +
38 ", picon='" + picon + '\'' +
39 ", plocation='" + plocation + '\'' +
40 ", pviews=" + pviews +
41 '}';
42 }
43 }

Product

 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" xmlns:util="http://www.springframework.org/schema/util"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
5
6 <bean id="u1" class="com.spring.entity.Users">
7 <property name="id" value="1"></property>
8 <property name="username" value="张三"></property>
9 <property name="password" value="123"></property>
10 <property name="cart">
11 <bean class="com.spring.entity.Cart">
12 <property name="id" value="1"></property>
13 <property name="uid" value="1"></property>
14 <property name="pcount" value="30"></property>
15 </bean>
16 </property>
17 </bean>
18
19 <bean id="c1" class="com.spring.entity.Cart">
20 <property name="id" value="1"></property>
21 <property name="uid" value="1"></property>
22 <property name="pcount" value="20"></property>
23 <property name="productSet" >
24 <set>
25 <ref bean="p1"></ref>
26 <ref bean="p2"></ref>
27 <ref bean="p3"></ref>
28 </set>
29 </property>
30 <property name="productMap" ref="mapproduct"></property>
31
32
33 </bean>
34
35 <bean id="p1" class="com.spring.entity.Product">
36 <constructor-arg name="id" value="1"></constructor-arg>
37 <constructor-arg name="pname" value="手机1"></constructor-arg>
38 <constructor-arg name="pprice" value="999"></constructor-arg>
39 <constructor-arg name="pinventory" value="10000"></constructor-arg>
40 <constructor-arg name="picon" value="图片地址"></constructor-arg>
41 <constructor-arg name="plocation" value="北京"></constructor-arg>
42 <constructor-arg name="pviews" value="582"></constructor-arg>
43 </bean>
44 <bean id="p2" class="com.spring.entity.Product">
45 <constructor-arg name="id" value="1"></constructor-arg>
46 <constructor-arg name="pname" value="手机2"></constructor-arg>
47 <constructor-arg name="pprice" value="999"></constructor-arg>
48 <constructor-arg name="pinventory" value="10000"></constructor-arg>
49 <constructor-arg name="picon" value="图片地址"></constructor-arg>
50 <constructor-arg name="plocation" value="北京"></constructor-arg>
51 <constructor-arg name="pviews" value="582"></constructor-arg>
52 </bean>
53 <bean id="p3" class="com.spring.entity.Product">
54 <constructor-arg name="id" value="1"></constructor-arg>
55 <constructor-arg name="pname" value="手机3"></constructor-arg>
56 <constructor-arg name="pprice" value="999"></constructor-arg>
57 <constructor-arg name="pinventory" value="10000"></constructor-arg>
58 <constructor-arg name="picon" value="图片地址"></constructor-arg>
59 <constructor-arg name="plocation" value="北京"></constructor-arg>
60 <constructor-arg name="pviews" value="582"></constructor-arg>
61 </bean>
62
63 <util:map id="mapproduct" >
64 <entry key="1">
65 <ref bean="p1"></ref>
66 </entry>
67 <entry key="2">
68 <ref bean="p2"></ref>
69 </entry>
70 </util:map>
71
72
73
74
75
76
77 </beans>

applicationContext.xml

 1 package com.spring.test;
2
3 import com.spring.entity.Cart;
4 import com.spring.entity.Users;
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 public class TestSpring {
9 public static void main(String[] args) {
10 System.out.println("----Users对象的创建----------");
11 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
12 Users user1 = (Users) ac.getBean("u1");
13 System.out.println(user1);
14
15
16 System.out.println("-------购物车对象的创建-------");
17 Cart cart1 = (Cart) ac.getBean("c1");
18 System.out.println(cart1);
19 }
20 }

TestSpring

 

构造器注入的方式给Cart属性赋值 关系1:1;1:n的更多相关文章

  1. java 面向对象(九):类的结构:构造器(一)简介;属性赋值顺序;JavaBean的概念

    1.构造器(或构造方法):Constructor构造器的作用: * 1.创建对象 * 2.初始化对象的信息2.使用说明: * 1.如果没显式的定义类的构造器的话,则系统默认提供一个空参的构造器 * 2 ...

  2. 深入源码分析Spring中的构造器注入

    # 1. 示例 构造器注入类,分别有三个构造器,一个是无参构造器,一个是注入一个Bean的构造器,一个是注入两个Bean的构造器: public class ConstructorAutowiredT ...

  3. Spring 依赖注入的方式

    Spring 支持3中依赖注入的方式 1.属性注入  通过setter 方法注入Bean的属性或依赖的对象. <bean id = " " class = " &q ...

  4. Spring构造器注入、set注入和注解注入

    记得刚开始学spring的时候,老师就反复的提到依赖注入和切面,平常的java开发中,在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,sp ...

  5. Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)

    什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...

  6. Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式

    配置Bean的形式可以基于XML文件的方式,也可以基于注解的方式,而Bean的配置方式可以通过全类名(反射),通过工厂方式和FactoryBean. XML形式 <?xml version=&q ...

  7. 依赖注入的方式(DI)

    方式: 接口注入: setter方法注入: 构造方法注入: 接口注入: public class ClassA { private InterfaceB clzB; public void doSom ...

  8. SpringBoot 构造器注入、Setter方法注入和Field注入对比

    0. 引入 今天在看项目代码的时候发现在依赖注入的时候使用了构造器注入,之前使用过 Field 注入和 Setter 方法注入,对构造器注入不是很了解.经过查阅资料看到,Spring 推荐使用构造器注 ...

  9. 【Spring】浅谈spring推荐构造器注入

    一.前言 ​ Spring框架对Java开发的重要性不言而喻,其核心特性就是IOC(Inversion of Control, 控制反转)和AOP,平时使用最多的就是其中的IOC,我们通过将组件交由S ...

随机推荐

  1. 计算机的网络参考模型与5G协议

    计算机的网络参考模型与5G协议    1 分层思想 2  OSI参考模型 3  TCP/IP 协议族的组成 4  数据的封装与解封 5 层间通讯过程 6  空口协议 1.喝可乐的人不一定知道其生产的过 ...

  2. MySQL 快速入门(一)

    目录 MySQL快速入门 简介 存储数据的演变过程 数据库分类 概念介绍 MySQL安装 MySQL命令初始 环境变量配置 MySQL环境变量配置 修改配置文件 设置新密码 忘记密码的情况 基本sql ...

  3. 10、Linux基础--find、正则、文本过滤器grep

    笔记 1.晨考 1.每个月的3号.5号和15号,而且这天是星期六时执行 00 00 3,5,15 * 6 2.每天的3点到15点,每隔3分钟执行一次 */3 3-15 * * * 3.每周六早上2点半 ...

  4. Realtime Data Processing at Facebook

    概要 这篇论文发表于2016年,主要是介绍Facebook内部的流式计算平台的设计与思考,对于流式计算的关键特性的实现选型上进行深度对比分析. 流式计算系统5个衡量指标 文中提到有5个重要的考量部分 ...

  5. [源码解析] NVIDIA HugeCTR,GPU版本参数服务器--- (6) --- Distributed hash表

    [源码解析] NVIDIA HugeCTR,GPU版本参数服务器--- (6) --- Distributed hash表 目录 [源码解析] NVIDIA HugeCTR,GPU版本参数服务器--- ...

  6. python2发微信脚本

    #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib,urllib2,json import sys reload(sys) sys. ...

  7. WebGL 与 WebGPU比对[5] - 渲染计算的过程

    目录 1. WebGL 1.1. 使用 WebGLProgram 表示一个计算过程 1.2. WebGL 没有通道 API 2. WebGPU 2.1. 使用 Pipeline 组装管线中各个阶段 2 ...

  8. [转自Matrix67] 趣题:顶点数为多少的图有可能和自己互补

    若干个顶点以及某些顶点和顶点之间的连线,就构成了一个"图".如果对某个图进行变换,使得原来任意两个有连线的顶点之间都不再有连线,原来任意两个没有连线的顶点之间现在都有连线了,那么所 ...

  9. 大数据时代变局与机遇,BI数字化转型的实战攻略!

    党的十九大报告提出,要推动互联网.大数据.人工智能和实体经济深度融合.更加高效地获取.运用信息,成为企业具有强大竞争力的重要标志.我国企业应牢牢把握历史性机遇,以更加开放的姿态,积极拥抱新经济,积极参 ...

  10. .Net Core之JWT授权

    一.什么是JWT 文章参考:https://www.leo96.com/article/detail/55 JSON Web令牌(JWT)是一个开放标准(RFC 7519),它定义 了一种紧凑且自包含 ...