为了账号安全,请及时绑定邮箱和手机立即绑定

Spring之路(6)--让周杰伦和林俊杰来给我们做代言(详解Spring中使用xml定义bean时到底发生了啥)

标签:
Spring

背景

之前讲过了如何使用xml定义spring容器中的bean,简简单单几行代码就可以实现,但是后面的道理可没那么简单,今天咱们就来好好絮叨絮叨,xml定义bean时到底发生了啥。

round1 容器还不存在

最开始的时候,我们使用xml定义下我们的容器,但是容器中啥也没有。

<?xml version="1.0" encoding="UTF-8"?><!-- spring.xml文件-->
<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.xsd">
	<!-- 容器中啥也没有 -->
</beans>

注意此时程序都没运行,所以内存中是混沌状态,如下:
图片描述

round2 空的容器

当我们运行下面一行代码时,spring框架就会为我们生成一个容器,因为spring.xml中并未定义bean,所以这时候会生成一个空的容器,里面没有任何bean(对象),context实际上就是指向容器的引用。

package org.maoge.xmlbeandetail;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"/org/maoge/xmlbeandetail/spring.xml");//注意,说的就是这一行
	}
}

总结下,上面一行代码的意思就是根据spring.xml的定义,生成一个容器对象context,此时内存中的状态如下:

图片描述

round3 要办年会,搞什么节目捏

要么就唱歌、要么就请几个讲相声的,最后领导拍板还是请歌手吧。来定义一个歌手类如下,普普通通的歌手类,没啥好说的。

package org.maoge.xmlbeandetail;
public class Singer {
	private String name;
	public void sing() {
		System.out.println("歌手[" + name + "]开唱啦,快挥舞起你手中的荧光棒吧");
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

此时容器中依然还是空的哦!世界上虽然有很多歌手,但是到目前为止跟咱们的容器没有任何关系!

round4 确定邀请哪些歌手

要请歌手,公司员工中80/90后居多,经过全体员工投票,最后得分最高的是周杰伦和林俊杰。本公司真是土豪啊有钱啊牛X啊,为何如此厉害?(PS:主要是程序里面随便定义个数字赋值个几百万上亿都是小事,哈哈,程序员的世界程序员就是无敌!)

OK,我们在容器中定义一下要请的对象。

<?xml version="1.0" encoding="UTF-8"?><!-- spring.xml文件 -->
<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.xsd">
	<bean id="zhoujielun" class="org.maoge.xmlbeandetail.Singer">
		<property name="name" value="周杰伦"></property>
	</bean>
	<bean id="linjunjie" class="org.maoge.xmlbeandetail.Singer">
		<property name="name" value="林俊杰"></property>
	</bean>
</beans>

当再次运行ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "/org/maoge/xmlbeandetail/spring.xml");这一行代码时,spring框架会扫描spring.xml文件,发现已经配置了两个Singer类的对象,所以在容器生成时里面会添加两个对象,如下图:
图片描述

round5 嗨!唱起来

万事俱备,只欠东风了,行动起来。

package org.maoge.xmlbeandetail;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"/org/maoge/xmlbeandetail/spring.xml");
		// 杰伦来了
		Singer zhoujielun = context.getBean("zhoujielun", Singer.class);
		zhoujielun.sing();
		// 俊杰来了
		Singer linjunjie = context.getBean("linjunjie", Singer.class);
		linjunjie.sing();
	}
}

此时就是让内存中对象执行它们的方法就行了:
图片描述

人治改法治是巨大进步

通过xml文件定义容器和容器中的对象及对象的属性信息,然后启动容器时就会自动加载对象了,对象的整个生命周期由容器管理。对象的生成由程序员控制变为了容器控制,就像传说中的人治改为按文件规定(xml),非常规范,实际上好处多多,后续大家会慢慢体会到的!

元数据

实际上xml的数据是一种描述和定义,用来向Spring容器提供配置定义相关的信息,这种数据被称为元数据。

元数据可以是任意形式,例如xml、json、二进制数据都可以,只要能描述清楚信息就可以了。

Spring框架目前支持三种元数据:xml、annotation(注解)、java config(java配置),实际上就是三种描述Spring容器内信息的数据格式,可以根据用户自己的喜爱或者需要灵活使用。

就像出行,你既可以乘飞机也可以坐高铁,如果你开心,骑共享单车也不错。

而我,喜欢自驾游,老弟,稳!

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
软件工程师
手记
粉丝
1.5万
获赞与收藏
1523

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消