IOC自动装载有两种形式

<?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">

     <!-- ByName -->
    <bean id="person" class="com.sunjian.entity.Person" autowire="byName">
        <property name="id" value="1"></property>
        <property name="name" value="张三疯"></property>
    </bean>
    <bean id="car" class="com.sunjian.entity.Car">
        <property name="id" value="1"></property>
        <property name="brand" value="野马"></property>
    </bean>

</beans>
<?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">

    <!-- ByType -->
    <bean id="person" class="com.sunjian.entity.Person" autowire="byType">
        <property name="id" value="1"></property>
        <property name="name" value="欧阳锋"></property>
    </bean>
    <bean id="car2" class="com.sunjian.entity.Car">
        <property name="id" value="2"></property>
        <property name="brand" value="法拉利"></property>
    </bean>

</beans>

class

package com.sunjian.entity;

public class Car {
    private Integer id;
    private String brand;

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

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Car(){
        System.out.println("创建了Car对象");
    }

    public Car(Integer id, String brand) {
        this.id = id;
        this.brand = brand;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", brand='" + brand + '\'' +
                '}';
    }
}
package com.sunjian.entity;

public class Person {
    private Integer id;
    private String name;
    private Car car;

    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;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", car=" + car +
                '}';
    }
}

test class

package com.sunjian.test;

import com.sunjian.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author sunjian
 * @date 2020/3/14 15:17
 */
public class Test3 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("autowired.xml");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person);

        applicationContext = new ClassPathXmlApplicationContext("autowired2.xml");
        Person person2 = (Person) applicationContext.getBean("person");
        System.out.println(person2);
    }
}
创建了Car对象
Person{id=1, name='张三疯', car=Car{id=1, brand='野马'}}
创建了Car对象
Person{id=1, name='欧阳锋', car=Car{id=2, brand='法拉利'}}

OK.

Spring框架——IOC 自动装载的更多相关文章

  1. Spring框架IOC容器和AOP解析 非常 有用

    Spring框架IOC容器和AOP解析   主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面 ...

  2. 自己动手写Spring框架--IOC、MVC

    对于一名Java开发人员,我相信没有人不知道 Spring 框架,而且也能够轻松就说出 Spring 的特性-- IOC.MVC.AOP.ORM(batis). 下面我想简单介绍一下我写的轻量级的 S ...

  3. Spring框架IOC容器和AOP解析

    主要分析点: 一.Spring开源框架的简介  二.Spring下IOC容器和DI(依赖注入Dependency injection) 三.Spring下面向切面编程(AOP)和事务管理配置  一.S ...

  4. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  5. spring Bean类自动装载实现

    先贴spring的开发文档,有助于大家学习http://shouce.jb51.net/spring/beans.html#beans-factory-class 一直想研究一下spring bean ...

  6. Spring框架IOC,DI概念理解

    1.什么是框架? 框架是一种重复使用的解决方案,针对某个软件开发的问题提出的. Spring框架,它是一个大型的包含很多重复使用的某个领域的解决方案. Spring的理念:不要重复发明轮子. 2.Sp ...

  7. Spring框架-IOC和AOP简单总结

    参考博客: https://blog.csdn.net/qq_22583741/article/details/79589910 1.Spring框架是什么,为什么,怎么用 1.1 Spring框架是 ...

  8. Spring框架 IOC注解

    Spring框架的IOC之注解方式的快速入门        1. 步骤一:导入注解开发所有需要的jar包        * 引入IOC容器必须的6个jar包        * 多引入一个:Spring ...

  9. Spring框架IOC和AOP介绍

    说明:本文部分内容参考其他优秀博客后结合自己实战例子改编如下 Spring框架是个轻量级的Java EE框架.所谓轻量级,是指不依赖于容器就能运行的.Struts.Hibernate也是轻量级的. 轻 ...

随机推荐

  1. zookeeper ACL(access control lists)权限控制

    基本作用:        针对节点可以设置 相关读写等权限,目的为了保障数据安全性        权限permissions可以制定不同的权限范围以及角色 一:ACL构成         zk的acl ...

  2. 谈谈从事IT测试行业的我,对于买房买车有什么样的感受

    周边测试同事,开发同事买?买?的比较多, 偶尔大家话题中也会谈起这个. 毕竟工作.衣.食.住.行和我们每个IT从业者息息相关, 大家有着相同或相似的感受与经验. - 前公司 以前公司测试经理 10年从 ...

  3. 在CodaLab上提交MURA竞赛的结果

    What is MURA? MURA (musculoskeletal radiographs) is a large dataset of bone X-rays. Algorithms are t ...

  4. iOS多线程之Thread

    多线程 • Thread 是苹果官方提供的,简单已用,可以直接操作线程对象.不过需要程序员自己管理线程的生命周期,主要是创建那部分 优缺点 面向对象,简单易用 直接操作线程对象 需要自己管理线程生命周 ...

  5. Git的安装与TortoiseGit的安装和汉化

    下载Git 进入https://git-scm.com/downloads 可以看到如下界面 因为我是windows系统,选择windows即可. 有的朋友因为网络慢的一些原因不能很快下载下来,可以进 ...

  6. Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解

    题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...

  7. symfony 5.05 dev安装为了更好的迭代更新

    我的项目目录 安装命令  composer create-project symfony/website-skeleton:^5.0.x-dev manage 数据查询测试输出

  8. windows 10 右键菜单注册表位置

    1. 查找 1.1. 打开注册表 # 1. 使用快捷键打开 “运行” win + r # 2. 在 “运行” 中输入 regedit # 3. 回车 1.2. 点击 查找 # 1. 方法 1 : 点击 ...

  9. [Python之路] object类中的特殊方法

    一.object类的源码 python版本:3.8 class object: """ The most base type """ # d ...

  10. 20170813-CSRF 跨站请求伪造

    CSRF CSRF是Cross Site Request Forgery的缩写,翻译过来就是跨站请求伪造. 跨站:顾名思义,就是从一个网站到另一个网站. 请求:即HTTP请求. 伪造:在这里可以理解为 ...