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. uos系统离线状态下进入开发者模式

    需到处机器信息,接着登入指定的uos开发者网站,下载证书,然后在机器上加载证书,重启即可.

  2. logger模块和re模块总结

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...

  3. Apache虚拟主机的搭建及相关问题解决

    在开发的过程中,很多时候项目的部署都需要在本地进行虚拟服务器的模拟搭建,所以具体的配置流程为下,并且把自己遇到的问题跟大家分享. 1.Apache配置文件httpd.conf 找到   # Virtu ...

  4. Solution -「AGC 019F」「AT 2705」Yes or No

    \(\mathcal{Description}\)   Link.   有 \(n+m\) 个问题,其中 \(n\) 个答案为 yes,\(m\) 个答案为 no.每次你需要回答一个问题,然后得知这个 ...

  5. Windows server 2016 2019远程端口修改操作

    windows server 2016 2019修改远程端口操作   一.修改3389远程端口 1,按"win+r"快捷键,在对话框中输入regedit 2, 找到路径 \HKEY ...

  6. 开源爱好者月刊《HelloGitHub》第 71 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  7. TestNG基本使用

    TestNG简介 Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation 创建maven项目,添加依赖 <dependency> ...

  8. 二十七 集合!!!!!!!!set 二十八 文件

    0: 不知道啊 去除重复元素? 1:frozenset 2:len(x) 3:直接一个错的报 4:好像一样       错  完全不一样的说  set = {[1,2]}这是想干嘛  :列表怎么可以进 ...

  9. SqlServer 局域网内不能连接对方数据库?

    一直都是连接的远程测试服务器的数据库,今天想把自己的数据库开放出来让公司同事连接,竟然连接失败!转了很大一个圈终于搞定了. 接下来就把这次心历路程发出来,希望能帮助到有需要的博友. PS: 我和同事的 ...

  10. 解决gpg failed to sign the data fatal: failed to write commit object解决方案

    今天有位新同事在comit代码的时候一直报这个错误: gpg failed to sign the data fatal: failed to write commit object. 看到网上说gp ...