1. 创建相关的类(这里是直接在之前类的基础上进行修改)

    package com.guan.dao;
    
    public interface Fruit {
    String getFruit();
    }
    package com.guan.dao;
    
    public class FruitImpl implements Fruit {
    public String getFruit() {
    return "Buy Some fruit";
    }
    }
    package com.guan.service;
    
    import com.guan.dao.Fruit;
    
    public interface UserService {
    String buyFruit();
    void setFruit(Fruit fruit);
    }
    package com.guan.service;
    
    import com.guan.dao.Apple;
    import com.guan.dao.Fruit;
    import com.guan.dao.FruitImpl; public class UserServiceImpl implements UserService{
    Fruit fruit; public UserServiceImpl() {
    fruit = new FruitImpl();
    } public String buyFruit() {
    return fruit.getFruit();
    } public void setFruit(Fruit fruit){
    this.fruit = fruit;
    } }
  2. 在resources目录下添加配置文件: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
    https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="fruit" class="com.guan.dao.FruitImpl"></bean>
    <bean id="apple" class="com.guan.dao.Apple"></bean> <bean id="userService" class="com.guan.service.UserServiceImpl">
    <property name="fruit" ref="fruit"></property>
    </bean> </beans>

    注:

    (1).即便没有属性需要初始化也需要通过<bean>来对其进行实例化,而不再需要new

    (2).<property>的注入需要类中存在set方法

    (3).对于属性的赋值的两种方式

    • 对于基本类型的赋值可以用value属性
    • 对于对象类型可以用ref(reference)属性以及<bean>中的id初始化
  3. 创建并使用实例

    import com.guan.dao.Fruit;
    import com.guan.service.UserService;
    import com.guan.service.UserServiceImpl;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) {
    //create and configure beans
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    //retrieve configured instance
    UserService userService = (UserService) context.getBean("userService");
    System.out.println(userService.buyFruit());
    }
    }
  4. 其它相关配置

  5. alias:给bean起别名,可以用多个不同的别名取出同一个类的实例

  6. bean

    (1).id:bean的唯一标识符

    (2).class:bean对象所应的全限定名(包名+类名)

    (3).name:也是别名,可以取同时取多个别名

  7. import:用于团队开发使用,可以将多个配置文件导入合并为一个.那么在调用时只要导入一个配置文件即可

spring——通过xml文件配置IOC容器的更多相关文章

  1. Spring中xml文件配置也可以配置容器list、set、map

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. SSM框架中spring的XML文件配置

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  3. Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有很大不同,多了很多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"?> ...

  4. Spring(十二)使用Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有非常大不同,多了非常多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"? & ...

  5. 重新学习之spring第一个程序,配置IOC容器

    第一步:导入相关jar包(此范例导入的是spring3.2.4版本,spring2.5版本只需要导入spring核心包即可) 第二步:在项目的src下配置applicationContext.xml的 ...

  6. 【spring源码分析】IOC容器解析

    参考: https://www.iteye.com/topic/1121913(自动注入bean的扫描器) https://m.imooc.com/mip/article/34150(循环依赖的解决方 ...

  7. Spring框架入门之基于xml文件配置bean详解

    关于Spring中基于xml文件配置bean的详细总结(spring 4.1.0) 一.Spring中的依赖注入方式介绍 依赖注入有三种方式 属性注入 构造方法注入 工厂方法注入(很少使用,不推荐,本 ...

  8. Spring整合Hibernate的XML文件配置,以及web.xml文件配置

    利用Spring整合Hibernate时的XML文件配置 applicationContext.xml <?xml version="1.0" encoding=" ...

  9. Spring 在xml文件中配置Bean

    Spring容器是一个大工厂,负责创建.管理所有的Bean. Spring容器支持2种格式的配置文件:xml文件.properties文件,最常用的是xml文件. Bean在xml文件中的配置 < ...

随机推荐

  1. Docker consul的容器服务更新与发现

    Docker consul的容器服务更新与发现 目录 Docker consul的容器服务更新与发现 一.Consul简介 1. 服务注册与发现 2. consul概述 3. consul的两种模式 ...

  2. Elementui【tooltip】 在弹框关闭之后再次‘出现’的问题

    如图,第一次弹窗进来的时候,符合条件之后,这个提示文字的位置是对的,而且正常显示: 现在点击取消按钮,第二次进入弹窗,如图,提示文字就跑到了左上角,而且输入符合条件的数值之后,会另外显示一个toolt ...

  3. 稳过!华为微认证ModelArts实现智能花卉识别稳过!

    华为微认证ModelArts实现智能花卉识别稳过! 目录 华为微认证ModelArts实现智能花卉识别稳过! ModelArts实现智能花卉识别的概述 ModelArts实现智能花卉识别的解决方案 M ...

  4. Docker prefereces

    https://docs.docker.com/docker-for-mac/#preferences-menu docker 的镜像命令需要抽时间了解

  5. 基于XC7Z100+AD9361的双收双发无线电射频板卡

    一.板卡概述 板卡基于Xilinx公司的SoC架构(ARM+FPGA)的ZYNQ7100芯片和ADI公司高集成度的捷变射频收发器AD9361,实现频谱范围70MHz~6GHz,模拟带宽200KHz~5 ...

  6. 矩阵QR分解

    1 orthonormal 向量与 Orthogonal 矩阵 orthonormal 向量定义为 ,任意向量  相互垂直,且模长为1: 如果将  orthonormal 向量按列组织成矩阵,矩阵为  ...

  7. Solution -「LOCAL」ZB 平衡树

    \(\mathcal{Description}\)   OurOJ.   维护一列二元组 \((a,b)\),给定初始 \(n\) 个元素,接下来 \(m\) 次操作: 在某个位置插入一个二元组: 翻 ...

  8. [Golang]一些书城项目中出现错误的原因和解决办法(三)

    跟着B站尚硅谷的GoWeb教程写书城项目,整理一下自己写的时候出现的错误和解决办法. 错误五:订单管理界面无法显示订单内容. 解决办法:我是直接把 day06 里的 order 文件夹粘贴过来了,or ...

  9. Linux C/C++ UDP 网络通信

    昨晚 Vv 想让我给她讲讲网络编程,于是我就傻乎乎的带她入了门... 以下内容为讲课时制作的笔记- 1. socket() 函数 1.1 头文件 #include<sys/socket.h> ...

  10. pytest--conftest.py用法(单独存放fixtrue配置的一个文件)

    1.conftest.py文件是什么 单独存放fixtrue配置的一个文件 用处是可以多个不同的pytest测试脚本共用一套fixture配置,代码维护方便 2.conftest.py使用注意事项 c ...