1、项目截图

2、创建xml文件

3、打印机接口

package com.example.demo.computerTest;

public interface Printer {
void init();
void print(String txt);
}

4、彩色打印机

package com.example.demo.computerTest;

public class ColorPrinter implements Printer {
@Override
public void init() {
System.out.println("启动彩色打印机!");
} @Override
public void print(String txt) {
System.out.println("打印彩色文字:".concat(txt));
}
}

5、电脑类

package com.example.demo.computerTest;

public class Computer {
String manu;
String type;
Printer p; public String getManu() {
return manu;
} public void setManu(String manu) {
this.manu = manu;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
}

  //printTxt()方法接收一个参数给打印机的print()方法实现打印的功能
public void printTxt(String txt){
p.init();
p.print(txt);
} public Printer getP() {
return p;
} public void setP(Printer p) {
this.p = p;
}
}

6、测试类

说明:

  通过ClassPathXmlApplicationContext载入XML文件

  通过向context.getBean()方法中传入参数,获取具体的bean,这个参数就是XML文件中的id名;

  通过实例对象p可以调用Computer类中的方法,可以获取配置文件中为Computer类属性设置的值。

package com.example.demo.computerTest;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class computerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("computers.xml");
Computer p = (Computer)context.getBean("pc");
p.printTxt("Hello,spring!");
System.out.println(p.getManu());
System.out.println(p.getType());
}
}

7、xml配置

说明:

  通过id属性给每个Bean设置id;

  通过class属性设置Bean的位置

  通过ref属性可以引用已经定义好的bean

  通过property可以操作bean中的属性:

    name属性指定bean中的某个属性

    value为该属性设置指定的值

<?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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="colorPrinter" class="com.example.demo.computerTest.ColorPrinter"/>
<bean id="pc" class="com.example.demo.computerTest.Computer">
<property name="manu">
<value>苹果</value>
</property>
<property name="type" value="IPad"/>
<property name="p" ref="colorPrinter"/>
</bean>
</beans>

8、效果:




1、测试类还可以是下面的代码

package com.example.demo.computerTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class computerTest {
public static void main(String[] args) {
//ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("computers.xml");
//Computer p = (Computer)context.getBean("pc");
//p.printTxt("Hello,spring!");
//System.out.println(p.getManu());
//System.out.println(p.getType()); ApplicationContext context = new ClassPathXmlApplicationContext("computers.xml");
Computer p = (Computer) context.getBean("pc");
p.printTxt("Hello,Spring!"); }
}

2、效果:

spring Ioc容器之使用XML配置Bean的更多相关文章

  1. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  2. 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean

    [spring以及第三方jar的案例]在spring中的aop相关配置的标签,线程池相关配置的标签,都是基于该种方式实现的.包括dubbo的配置标签都是基于该方式实现的.[一]原理 ===>sp ...

  3. spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean

    5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...

  4. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(6):Spring IOC容器学习(概念、作用、Bean生命周期)

    一.IOC控制反转概念 控制反转(IOC)是一种通过描述(在Java中可以是XML或者是注解)并通过第三方去生产或获取特定对象的方式. 主动创建模式,责任在于开发者,而在被动模式下,责任归于Ioc容器 ...

  5. Spring IoC 容器和 bean 对象

    程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...

  6. Spring IoC容器总结(未完)

    在面向对象系统中,对象封装了数据和对数据的处理,对象的依赖关系常常体现在对数据和方法的依赖上.这些依赖关系可以通过把对象的依赖注入交给框架或IOC容器来完成,这种从具体对象手中交出控制的做法是非常有价 ...

  7. 学习Spring(一) 实例化Spring IoC容器

    实例化Spring IoC容器 1,读取其配置来创建bean实例 2,然后从Spring IoC容器中得到可用的bean实例 Spring提供两种IoC容器实现类型 a,一种为bean工厂 b,应用程 ...

  8. Spring IOC 源码简单分析 02 - Bean Reference

    ### 准备 ## 目标 了解 bean reference 装配的流程 ##测试代码 gordon.study.spring.ioc.IOC02_BeanReference.java   ioc02 ...

  9. Spring IOC 容器预启动流程源码探析

    Spring IOC 容器预启动流程源码探析 在应用程序中,一般是通过创建ClassPathXmlApplicationContext或AnnotationConfigApplicationConte ...

随机推荐

  1. 后台获取POST方式提交的JSON格式数据

    1.直接使用request.getParamater()的方法获取(这种取参方式对于POST和GET的提交方式均适用): 2.通过请求体的IO流获取参数(这种方式只能用于POST,因为GET方式没有请 ...

  2. C# Finalize和Dispose的区别

    一:总结 1.Finalize方法(C#中是析构函数,以下称析构函数)是用于释放非托管资源的,而托管资源会由GC自动回收.所以,我们也可以这样来区分 托管和非托管资源.所有会由GC自动回收的资源,就是 ...

  3. 「Python」数据清洗常用正则

    对爬虫数据进行自然语言清洗时用到的一些正则表达式 标签中的所有属性匹配(排除src,href等指定参数) 参考链接 # \b(?!src|href)\w+=[\'\"].*?[\'\&quo ...

  4. Python学习笔记_1

    一.基础知识 1. 基础: 数据结构:常量.变量.元组(列表).字典 数据类型:int float str 算法:分支和循环 面向对象:类.属性.方法 封装.继承.多态 2. python语言的特点: ...

  5. linux blast

    建库 减压后,改名为blast,并在blas目录在建立db文件1,建立数据库makeblastdb -in db.fasta -dbtype nucl(prot) -parse_seqids -has ...

  6. urllib库详解 --Python3

    相关:urllib是python内置的http请求库,本文介绍urllib三个模块:请求模块urllib.request.异常处理模块urllib.error.url解析模块urllib.parse. ...

  7. 如何增加亚马逊listing多个类目节点

    流量是电商销售的必要因素,可以说,任何成功的电商平台都离不开流量.亚马逊listing优化做得好,不仅能提高产品的曝光率,还能提升转换率,而好的类目可以吸引大的流量.帮你快速爬升. 首先我们来了解一下 ...

  8. 基于apache的虚拟主机

    apche支持3种类型虚拟主机: 基于IP的虚拟主机 基于端口的虚拟主机 基于主机名的虚拟主机 配置两个站点虚拟主机: /data/web1 /data/web2 一.基于IP虚拟主机 添加一个IP: ...

  9. OO-第二单元总结

    一.三次作业的设计策略 (1). 第五次作业 第五次作业由于较为简单,在强测及互测中均没有出现BUG,但是并没有做优化.本次的设计有些不合理,所以在后面的作业中也做了重构.本次的作业主要有三个类,主函 ...

  10. ArcGIS中删除“点”附带的对应“文本信息”

    现状: 用ArcMap打开对应的.mxd文件,导入KML数据后,几何类型“点” - 每一个点都附带对应的文本信息“Placemark”,如下图: 问题:ArcGIS中如何 删除“点”附带的对应“文本信 ...