当前位置: 首页 > news >正文

spring IOC bean中注入bean

俩个实体

package com.java.test4;

/**
 * @author nidegui
 * @create 2019-06-22 14:45
 */
public class People {
    private Integer id;
    private String name;
    private String age;
    private Dog dog;

    public Integer getId() {
        return id;
    }

    public People() {
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", dog=" + dog +
                '}';
    }

    public People(Integer id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

  

package com.java.test4;

/**
 * @author nidegui
 * @create 2019-06-22 15:37
 */
public class Dog {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return   " {name=" + name+"}" ;
    }
}

 

beans.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.xsd">

	<bean id="dog1" class="com.java.test4.Dog">
		<property name="name" value="javk"></property>
 	</bean>
	<bean id="people" class="com.java.test4.People">
		<property name="name" value="nidegui"></property>
		<property name="id" value="1"></property>
		<property name="age" value="12"></property>
		<property name="dog" ref="dog1"></property>
	</bean>

</beans>

  

测试:

package com.java.test4;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author nidegui
 * @create 2019-06-22 14:47
 */
public class Test {
    public static void main(String[] args) {
       ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        People people =(People) ac.getBean("people");
        System.out.println(people);
    }
}

  

 

转载于:https://www.cnblogs.com/nidegui/p/11069133.html

相关文章:

  • 【OpenJ_Bailian - 4110】圣诞老人的礼物-Santa Clau’s Gifts (贪心)
  • centos7通过yum安装docker
  • 【Beta】Scrum meeting 2
  • 在Windows下搭建Gitlab服务器
  • mysql 是如何保证在高并发的情况下autoincrement关键字修饰的列不会出现重复
  • Docker是什么?可以用Docker做什么?
  • 《坐热板凳》第九次团队作业:Beta冲刺与验收准备(补交:实验十二 第八次团队作业:软件测试与ALPHA冲刺)...
  • 14-使用Vue来实现JQuery的动画效果
  • MP4V2 移植 (基于imx6 平台)
  • python学习之模块--模块(二)
  • Java数据结构和算法(七)--AVL树
  • linux 获取系统时间 strftime函数格式化时间为24/12小时制
  • 第7章 虚拟机类加载机制
  • Redis 学习笔记(篇三):跳表
  • Origin C访问数据库(MySQL)
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • 2017-08-04 前端日报
  • canvas 高仿 Apple Watch 表盘
  • ERLANG 网工修炼笔记 ---- UDP
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • javascript 总结(常用工具类的封装)
  • Javascript设计模式学习之Observer(观察者)模式
  • Java知识点总结(JavaIO-打印流)
  • nfs客户端进程变D,延伸linux的lock
  • Spring Cloud(3) - 服务治理: Spring Cloud Eureka
  • windows下mongoDB的环境配置
  • 诡异!React stopPropagation失灵
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 普通函数和构造函数的区别
  • 时间复杂度与空间复杂度分析
  • 网页视频流m3u8/ts视频下载
  • 微信开放平台全网发布【失败】的几点排查方法
  • 一份游戏开发学习路线
  • 译米田引理
  • ​【已解决】npm install​卡主不动的情况
  • #100天计划# 2013年9月29日
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • $.proxy和$.extend
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (NSDate) 时间 (time )比较
  • (阿里云万网)-域名注册购买实名流程
  • (待修改)PyG安装步骤
  • (分布式缓存)Redis持久化
  • (论文阅读22/100)Learning a Deep Compact Image Representation for Visual Tracking
  • (一)u-boot-nand.bin的下载
  • (一)使用IDEA创建Maven项目和Maven使用入门(配图详解)
  • (转)大道至简,职场上做人做事做管理
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .net core webapi 大文件上传到wwwroot文件夹
  • .net6Api后台+uniapp导出Excel
  • .netcore如何运行环境安装到Linux服务器
  • .net反混淆脱壳工具de4dot的使用
  • .NET实现之(自动更新)
  • .Net下C#针对Excel开发控件汇总(ClosedXML,EPPlus,NPOI)