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. Net6 DI源码分析Part3 CallSiteRuntimeResolver,CallSiteVisitor

    CallSiteRuntimeResolver CallSiteRuntimeResolver是实现了CallSiteVisitor之一. 提供的方法主要分三个部分 自有成员方法 Resolve提供服 ...

  2. VMware Workstation批量克隆虚拟机

    由于经常要用vmware创建虚拟机做一些测试,集群的测试使用连接克隆,可以节省磁盘的空间(如果不是因为穷,没人愿意向生活低头) 于是找到了这个bat脚本,做了一些修改和学习,为大家加上了一些注释,方便 ...

  3. matlab文件处理

    1.读取文件(按行读取) fid = open('file_name');while(~feof(fid)) line = fgetl(fid); % 读取一行数据 endfid.close(); 2 ...

  4. 『无为则无心』Python面向对象 — 60、魔法属性

    目录 1.魔法属性__name__ 2.魔法属性__bases__ 3.魔法属性__mro__ 4.魔法属性__doc__ 5.魔法属性__module__ 和__class__ 6.魔法属性__di ...

  5. MySQL让人又爱又恨的多表查询

    1. 前言 在SQL开发当中,多表联查是绝对绕不开的一种技能.同样的查询结果不同的写法其运行效率也是千差万别. 在实际开发当中,我见过(好像还写过~)不少又长又臭的查询SQL,数据量一上来查个十几分钟 ...

  6. Win11右键菜单怎么修改为传统Win10右键风格?

    前言 对于开发者而言,右键菜单会集成一些功能,升级 Win11 的右键菜单反而对开发造成不便,所以修改回传统风格. 修改成果如下 两个步骤改回传统右键风格 第一步:首先用鼠标右键点击开始菜单,选择 w ...

  7. Idea项目添加新文件后运行出现404问题

    今天在项目里添加了一个新的html文件,然后运行项目页面跳转出现了404问题,找不到页面,经过我的一番查找,我注意到了Idea项目下有一个target文件,然后上网搜了解到这个target文件是mav ...

  8. MySQL 学习-进阶

    MySQL高级学习 一.MySQL 事务 1.1.事务的概念 一条或多条 SQL 语句组成一个执行单元,其特点是这个单元要么同时成功要么同时失败,单元中的每条 SQL 语句都相互依赖,形成一个整体,如 ...

  9. ABP 使用ElasticSearch、Kibana、Docker 进行日志收集

    ABP 使用ElasticSearch.Kibana.Docker 进行日志收集 后续会根据公司使用的技术,进行技术整理分享,都是干货哦别忘了关注我!!! 最近领导想要我把项目日志进行一个统一收集,因 ...

  10. python中的流程控制

    目录 引言 流程控制的分类 分支结构 单if结构 if与else结构 if与elif与else结构 if分支的嵌套 循环结构 while循环 while + break循环 while + conti ...