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. Python将py文件编译为exe的方法

    使用PyCharm工具写好的Python程序脚本,怎么将.py文件编译为可执行的.exe文件 前提是已经安装了Python环境. 第一步:在PyCharm内下载安装pyinstalle库或使用CMD安 ...

  2. 重新认识Appium

    一.重新认识Appium   找到了学习资料,却不知道怎么实现!!! 要如何实现呢? Appium完整案例值得参考:手把手搭建环境,其中安装和配置Mave这部分有点老了. 首先下载maven 官网地址 ...

  3. suging闲谈-netty 的异步非阻塞IO线程与业务线程分离

    前言 surging 对外沉寂了一段时间了,但是作者并没有闲着,而是针对于客户的需要添加了不少功能,也给我带来了不少外快收益, 就比如协议转化,consul 的watcher 机制,JAVA版本,sk ...

  4. 如何在 Xamarin 中快速集成 Android 版认证服务 - 邮箱地址篇

    Xamarin 作为微软提供的移动服务多系统开发平台,成为很多开发者首选的应用开发平台.AppGallery Connect(以下简称 AGC)也在逐步的支持 Xamarin 的 SDK.认证服务也是 ...

  5. 我们一起来学Shell - shell的并发及并发控制

    文章目录 bash的并发 未使用并发的脚本 简单修改 使用wait命令 控制并发进程的数量 文件描述符 查看当前进程打开的文件 自定义当前进程用描述符号操作文件 管道 我们一起来学Shell - 初识 ...

  6. CentOS7 下 ldap 部署

    环境准备 # 关闭防火墙以及selinux,生产环境中,以实际需求为准 [root@localhost ~]# hostnamectl --static set-hostname ldap-serve ...

  7. c++ 汇编代码看内存分配

    汇编代码看内存分配 (1). 程序运行时分为存储区域分为 存储区域 存储内容 extra 代码区 存放代码指令,包括除字符串常量的字面值 静态存储区 存放静态变量和全局变量 执行main之前就分配好了 ...

  8. nginx 配置ssl证书

    1.443端口配置 server { listen 443 ssl; server_name www.test.com; ssl_certificate /usr/local/nginx/cert/t ...

  9. Eclipse插件拓展点

    一.新建一个项目,不使用模板 二.增加"hello"拓展点 1. 打开插件描述文件的Extensions页 新建一个插件后,会自动打开插件清单文件编辑器,也可以通过META-INF ...

  10. python数据结构:链表

    链表与列表.数组这线性结构不同之处在于其在首末两端增删的话比较方便 单链表: 但是链表查找和删除的话都是需要从第一个开始从头查找 因此查找和删除的复杂度都为O(n) 双链表: 相比单链表来说,每个节点 ...