基础接口 BeanFactory、ApplicationContext。

BeanFactory用于创建并管理、获取各种类的对象。

ApplicationContext从BeanFactory派生而来,并拥有更多实际功能。主要方法是getBean()获取Bean对象。

ApplicationContext可以从类路径加载配置文件(ClassPathXmlApplicationContext),也可以从文件系统中加载配置文件(FileSystemXmlApplicationContext)。

我们可以在xml文件中配置bean,然后通过ApplicationContext接口获取bean实例

Spring能够在applicationContext.xml中通过配置bean完成实例化对象,设置属性等。

1.配置bean

<bean id="dao" class="springTest.DaoImpl">
</bean>

实例化对象,相当于以下代码:

DaoImpl dao=new DaoImpl();

我们可以通过ApplicationContext接口获取对象,如下示:

ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
DaoImpl dao = (DaoImpl) context.getBean("dao");

ClassPathXmlApplicationContext会读取src文件的下一级中的对应xml文件

2.配置属性property

<bean id="dao" class="springTest.DaoImpl">
<property name="employee" value="lin"></property>
</bean>

设置属性,也就是setter,相当于以下代码:

DaoImpl dao=new DaoImpl();
dao.setEmployee=lin;

3.配置对象属性property

<bean id="dao" class="springTest.DaoImpl"></bean>
<bean id="serviceImpl" class="springTest.ServiceImpl">
<property name="dao" ref="dao"></property>
</bean>

设置对象属性,相当于以下代码:

DaoImpl dao=new DaoImpl();
ServiceImpl serviceImpl=new ServiceImpl();
serviceImpl.setDao=dao;

Spring依赖注入:基于xml配置的更多相关文章

  1. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  2. Spring实现Ioc的多种方式--控制反转、依赖注入、xml配置的方式实现IoC、对象作用域

    Spring实现Ioc的多种方式 一.IoC基础 1.1.概念: 1.IoC 控制反转(Inversion of Control) IoC是一种设计思想. 2.DI 依赖注入 依赖注入是实现IoC的一 ...

  3. Spring 依赖注入 基于构造函数、设值函数、内部Beans、集合注入

    Spring 基于构造函数的依赖注入_w3cschool https://www.w3cschool.cn/wkspring/t7n41mm7.html Spring 基于构造函数的依赖注入 当容器调 ...

  4. 【原】Spring和Dubbo基于XML配置整合过程

    背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个 ...

  5. Spring依赖注入的简化配置

    一, 很久很久以前, 当我们不用@Autowire注解时, 依赖注入要么通过setter方法, 要么通过构造方法; 需要在配置文件里配置一大堆property-ref.......... 二, 若使用 ...

  6. Spring依赖注入 — util命名空间配置

    要使用<util>标签,必须在XML中加入util名称空间(namespace): xmlns:util="http://http://www.springframework.o ...

  7. Spring实战——无需一行xml配置实现自动化注入

    已经想不起来上一次买技术相关的书是什么时候了,一直以来都习惯性的下载一份电子档看看.显然,如果不是基于强烈的需求或强大的动力鞭策下,大部分的书籍也都只是蜻蜓点水,浮光掠影. 就像有位同事说的一样,有些 ...

  8. 【Spring Framework】Spring入门教程(二)基于xml配置对象容器

    基于xml配置对象容器--xml 标签说明 alias标签 作用:为已配置的bean设置别名 --applicationContext.xml配置文件 <?xml version="1 ...

  9. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  10. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

随机推荐

  1. 【Spring-AOP-学习笔记-5】@AfterReturning增强处理简单示例

    项目结构 业务代码 @Component("hello") public class HelloImpl implements Hello {     // 定义一个简单方法,模拟 ...

  2. 关于后台程序java报错问题解决

    HTTP Status – Internal Server Error Type Exception Report Message Handler processing failed; nested ...

  3. 一些css杂项笔记

    div[class*="col-"]{ background-color: gold; border: 1px solid #ccc; } //给class开头等于col-的div ...

  4. Find substring with K distinct characters

    Given a string and number K, find the substrings of size K with K distinct characters. If no, output ...

  5. Win CE 5.0 增加电池电量显示

    摘自 http://blog.csdn.net/li0531/article/details/8818243 同时只在右上角显示一个 Label 控件,因此代码量精简了很多. [DllImport(& ...

  6. 廖雪峰Java1-2Java程序基础-3整数运算

    1.四则运算规则 int i =(100 + 200) * (99 -88);//3300 int n = i + 9;//3309 //除法结果为整数 int q = n / 100;//33 // ...

  7. [UE4]事件代理,无输出参数,蓝图中不需要绑定

    .h UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName = "LoginSuccess")) void LoginS ...

  8. 使用php的mysqli扩展库操作mysql数据库

    简单介绍mysqli: 1.mysqli(mysql improve mysql扩展库的增强版) mysql扩展库和mysqli扩展库的区别 1.mysqli的稳定性  安全性 和 执行效率有所提高 ...

  9. php localeconv() 函数实例讲解

    php localeconv() 函数返回一包含本地数字及货币格式信息的数组.本文章向码农介绍php localeconv() 函数的使用方法和基本实例.需要的码农可以参考一下. 定义和用法 loca ...

  10. django中的 form 表单操作

     form组件  1. 能做什么事?   1. 能生成HTML代码  input框   2. 可以校验数据   3. 保留输入的数据   4. 有错误的提示   1. 定义   from django ...