对Category和Product注入属性,并且对Product对象,注入一个Category对象

一、新建项目

二、导包

三、新建Category类

package com.yyt.pojo;

public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

四、新建Product类,将添加一个Category类型属性

package com.yyt.pojo;

public class Product {
private int id;
private String name;
private float price;
private Category category;

五、在src目录下新建applicationContext.xml文件

要注入对象,需在property中添加ref=“该类在bean中的name”,例如本次的 “c”是Category在bean中的name值

<?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="c" class="com.yyt.pojo.Category">
<property name="id" value="1" />
<property name="name" value="category 1" /> </bean>
<bean name="p" class="com.yyt.pojo.Product">
<property name="id" value="1" />
<property name="name" value="product 1" />
<property name="price" value="8848" />
<property name="category" ref="c" />
</bean> </beans>

六、Test类

package com.yyt.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yyt.pojo.Category;
import com.yyt.pojo.Product; public class Test { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml"});
Category cg = (Category) context.getBean("c");
System.out.println("id:"+cg.getId()+" name:"+cg.getName()); Product p = (Product) context.getBean("p");
System.out.println("id:"+p.getId()+" name:"+p.getName()+" price:"+p.getPrice());
//输出注入的对象
System.out.println(p.getCategory().getName()); } }

注:基于框架的程序要成功运行,对于JAR包的版本,配置文件的正确性有着苛刻的要求,任何一个地方出错了,都会导致框架程序运行失败。

该项目上传了GitHub:https://github.com/yeyangtao/Spring

Spring注入属性、对象的更多相关文章

  1. spring注入 属性注入 构造器注入 set方法注入

    spring注入 属性注入 构造器注入 set方法注入(外部bean注入)

  2. spring 注入属性

    一.注入对象类型的数据 1.配置文件 User类与UserService类均需要创建对象.所以都配置其相应的bean类,另外user需作为userService的属性注入,所以userService需 ...

  3. Spring注入的对象到底是什么类型

    开篇 之前,在用spring编码调试的时候,有时候发现被自动注入的对象是原始类的对象,有时候是代理类的对象,那什么时候注入的原始类对象呢,有什么时候注入的是代理类的对象呢?心里就留下了这个疑问.后来再 ...

  4. spring学习——注入静态对象属性

    spring注入静态对象属性时,因为虚拟机类加载问题,直接在属性上使用@Autowired 是不可以的.需要在属性对应的set方法上@Autowired,并且,set方法不能定义为static. 1. ...

  5. 【Spring】---属性注入

    一.Spring注入属性(有参构造和[set方法]) 注意:在Spring框架中只支持set方法.有参构造方法这两种方法. 使用有参数构造方法注入属性(用的不多,但需要知道): 实体类 package ...

  6. day38 11-Spring的Bean的属性的注入:对象属性

    package cn.itcast.spring3.demo5; public class Person { private String name;//人的名字 private Car2 car2; ...

  7. IoC容器-Bean管理XML方式(创建对象和set注入属性,有参构造注入属性)

    Ioc操作Bean管理 1,什么是Bean管理 (0)Bean管理指的是两个操作 (1)Spring创建对象 (2)Spring注入属性 2,Bean管理操作有两种方式 (1)基于xml配置文件方式实 ...

  8. spring:为javabean的集合对象注入属性值

    spring:为JavaBean的集合对象注入属性值 在 spring 中可以对List.Set.Map 等集合进行配置,不过根据集合类型的不同,需要使用不同的标签配置对应相应的集合. 1.创建 Ts ...

  9. spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...

随机推荐

  1. vs2012解决scanf,printf编译出错的问题

    转自http://www.th7.cn/Program/c/201303/127343.shtml 在VS 2012 中编译 C 语言项目,如果使用了 scanf 函数,编译时便会提示如下错误: er ...

  2. BZOJ1568:[JSOI2008]Blue Mary开公司

    浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227.html 题目传送门:https://www.lydsy.com/JudgeOnline/proble ...

  3. python--面向对象(最全讲解)

    http://www.cnblogs.com/Eva-J/articles/7293890.html 阅读目录 楔子 面向过程vs面向对象 初识面向对象 类的相关知识 对象的相关知识 对象之间的交互 ...

  4. 重学JAVA基础(五):面向对象

    1.封装 import java.util.Date; public class Human { protected String name; protected BirthDay birthDay; ...

  5. session和cookie(2)

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  6. php + mssql乱码

    当用PHP自带的模块php_mssql.dll去调用MSSQL数据库时,中文数据会乱码.但如果我们采用ADODB的方式去做,就不会乱码了.请看下面的具体实例: 调用开源的adodb.inc.php(支 ...

  7. 2019计蒜之道初赛4 B. 腾讯益智小游戏—矩形面积交(简单)(矩形交集)

    B. 腾讯益智小游戏—矩形面积交(简单) 1000ms 262144K   腾讯游戏开发了一款全新的编程类益智小游戏,最新推出的一个小游戏题目是关于矩形面积交的.聪明的你能解出来吗?看下面的题目接招吧 ...

  8. 利用URL重写隐藏复杂的URL

    第一步:模拟映射页面 我们想在一个页面上点击guid.html链接,跳转到比较复杂URL的guid_{492f3e0b-848e-11da-9550-00e08161165f}.html页面.即定义一 ...

  9. 黑科技抢先尝(续2) - Windows terminal中Powershell Tab的极简美化指南

    目录 安装python 安装git 安装powerline字体 主题定制 安装oh-my-posh 查看策略组的执行权限 使用choco 安装终端模拟器 - ConEmu 优化 PowerShell ...

  10. 洛谷P2029 跳舞

    P2029 跳舞 题目描述 小明今天得到一个跳舞毯游戏程序Dance.游戏每次连续出N个移动的“箭头”,箭头依次标号为1到N,并且的相应的分数S[1..N].如果你能“踏中”第i号箭头,你将获得相应的 ...